0

Hello (my first post), I should write a program that checks if a sentence or a word is a Palindrome. For this I must use Stacks. The thing is that I print both solutions out in the console but even if they are the same it returns false.

My Code:

public class Palindrome {
  // Test if text is a palindrome.
  // Ignore upper/lower case, white space, and punctuation.
  //
  public static boolean isPalindrome(String text) {
    boolean ispalin =false;
    char tmp;
    String temp = text;
    String text2 = "";
    String check = "";
    Stack<Character> stacktext = new Stack<Character>();
    Stack<Character> stackcheck = new Stack<Character>();
    text = text.toLowerCase();



    for (int j = 0; j < text.length(); j++) {
      if(     text.charAt(j) != ' '&&
              text.charAt(j) != ','&&
              text.charAt(j) != '?'&&
              text.charAt(j) != '.'&&   //delete some symbols
              text.charAt(j) != '!'&&
              text.charAt(j) != '-'){


        tmp = text.charAt(j);
        text2 += tmp;
      }
    }



    for (int j = text.length()-1; j > -1; j--) {
      if(     text.charAt(j) != ' '&&
              text.charAt(j) != ','&&
              text.charAt(j) != '?'&&
              text.charAt(j) != '.'&&
              text.charAt(j) != '!'&&
              text.charAt(j) != '-'){


        tmp = text.charAt(j);
        check += tmp;
      }
    }
    for (int i = 0; i < text2.length(); i++) {
      stacktext.push(text2.charAt(i));
    }
    for (int i = check.length()-1; i != -1; i--) {
      stackcheck.push(check.charAt(i));
    }
    System.out.println(stackcheck);
    System.out.println(stacktext);
    if (stackcheck == stacktext) return true;
    else return false;
  }


  public static void main(String[] args) {
    System.out.println(isPalindrome("Na, Fakir, Paprika-Fan?"));
  }
}

The Output:

[n, a, f, a, k, i, r, p, a, p, r, i, k, a, f, a, n]
[n, a, f, a, k, i, r, p, a, p, r, i, k, a, f, a, n]
false
anastaciu
  • 23,467
  • 7
  • 28
  • 53
Detalafol
  • 19
  • 2

1 Answers1

1

The first problem is that your comparison

//...
if (stackcheck == stacktext) return true;
    else return false;
//...

will always evaluate to false, these are two different objects.

You need:

//...
if (stackcheck.equals(stacktext)) return true;
    else return false;
//

Or better yet:

//...
return stackcheck.equals(stacktext);
//...

The other problem is that the strings are placed in the Stack in the same order and not being reversed, therefore the comparison will always evaluate to true no matter if the strings are paindromes or not.

Working sample, using StringBuilder to reverse the second string check before adding it to stackcheck, of course you can do it yourself, adding it to the Stack in reverse order.

public static boolean isPalindrome(String text)
{
    text = text.toLowerCase();

    String text2 = text.replaceAll("[,. !?-]", ""); //replaceAll to remove characters
    String check = new StringBuilder(text2).reverse().toString(); //reverse string

    Stack<Character> stacktext = new Stack<>();
    Stack<Character> stackcheck = new Stack<>();

    for (int i = 0; i < text2.length(); i++)
    {
        stacktext.add(text2.charAt(i));
    }

    for (int i = 0; i < check.length(); i++)
    {
        stackcheck.add(check.charAt(i));
    }

    System.out.println(stackcheck);
    System.out.println(stacktext);

    return stackcheck.equals(stacktext);
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • i tested this but this didn't work either – Detalafol Apr 24 '20 at 13:51
  • i know now where the Problem was my Prof did create his own stack without an equal method but in the java doc stack there is an equal method. I used the Stack from my Prof. Thanks for the help – Detalafol Apr 24 '20 at 14:16
  • @Detalafol, I added a simple implementaton where you can see the differences between `equals` and `==` methods https://onlinegdb.com/rkWivdeFL – anastaciu Apr 24 '20 at 14:30
  • @Detalafol, so I guess the problem is that the strings are not reversed, so when you compare them they are allways equal. – anastaciu Apr 24 '20 at 14:35
  • @Detalafol, I added a solution to my answer, hope it helps. – anastaciu Apr 24 '20 at 15:00