Search code examples
javapalindromecomparison-operators

Palindrome Fail


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

Solution

  • 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);
    }