Every time I think I'm making progress, I get stumped. Can anyone give me a hint at what I'm doing wrong. I have to use a stack to check if user input is a palindrome. However, whatever I put in.... the printout says that it is a palindrome. I've tried if(true)....and also if(!true)....in my statement... Your help is greatly appreciated...
Your method getPhrase returns a boolean. At the line getPhrase( s ); You're calling that method to determine if the String sent in is a palindrome, but you're throwing away the result of the method call by not doing anything with that result. Plus if ( true ) is always going to be true because "true" is a boolean literal. There a two ways to fix this. You can assign the result of the method call to a some variable and then test the value of that variable in your if condition: boolean isPalindrome = getPhrase( s ) ; if ( isPalindrome ) ... But since you're not using that value anywhere else you can just do the method call directly in the condition: if ( getPhrase( s ) ) ... Also, "getPhrase" doesn't say a lot about what the method does. You might consider renaming to something like if ( isPalindrome( s ) ) ... *** You mentioned testing a "word," so I'm guessing it's okay for your purposes things like A man, a plan, a canal -- Panama! don't count as palindromes? (Things that contain characters other than letters or digits.) At any rate, you might want to consider elimnating case considerations. In your version "Mom" isn't a palindrome. String has a handy method that tests for equality, ignoring the case of letters.
return reverse.equals(s); // Returns true if s == reverse
Here your code is correct, but the comment is not. Without getting into all the gory details of object identity vs. object equality, reverse.equals(s) tests to see whether reverse and s are equal as defined by the equals method of String -- which is what you want here. s == reverse tests to see whether s and reverse point to the exact same object, which they do not. (After you get your version working, you could try testing s == reverse and see that it evaluates to false.)
Get me the mayor's office! I need to tell him about this tiny ad: