• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Palindromes

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how about if(getPhrase(s)) ???
 
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try:

Good Luck,
Ellen
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Don't MAKE me come back there with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic