This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Palindrome example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Palindrome example" Watch "Palindrome example" New topic
Author

Palindrome example

Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Dear friends,

I wrote the following code for checking a Palindrome (i.e. a string remains the same when reversed). However, it runs correctly only for one iteration of the loop, in the next iteration it takes the old value of the String from the console and does not wait for reading the new value. I cant understand why ? Kindly explain. The code is :


[ August 16, 2008: Message edited by: Anand Shrivastava ]
[ August 16, 2008: Message edited by: Anand Shrivastava ]

Anand Shrivastava
SCJA
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
One thing that looks wrong is the setWord method. It should call word.setLength(0) before appending the new value.
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Yes,i amended it. But even after that it does not wait for new input second time onwards.
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
My guess would be that the readLine reads the newline character that you need to type after the "y"/"n".

You may want to change the System.in.read call into a readLine as well, and then extract the first character from it.
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Yes Ulf, you are right. You guys are genius. I dont know from where you learnt so much. Anyway, amending as per your advise.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Palindrome example
 
It's not a secret anymore!