File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Palindromes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Palindromes" Watch "Palindromes" New topic


Lynn Finley

Joined: Sep 29, 2002
Posts: 13
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) my statement...
Your help is greatly appreciated...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
how about if(getPhrase(s)) ???

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581

Good Luck,
Michael Matola
Ranch Hand

Joined: Mar 25, 2001
Posts: 1793
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.
Michael Matola
Ranch Hand

Joined: Mar 25, 2001
Posts: 1793
One more thing

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.)
I agree. Here's the link:
subject: Palindromes
It's not a secret anymore!