Results 1 to 5 of 5
Thread: Palindrome problem
- 02-18-2009, 01:57 AM #1
Member
- Join Date
- Dec 2008
- Location
- Davao Oriental
- Posts
- 29
- Rep Power
- 0
Palindrome problem
import java.util.Scanner;
public class Palindrome {
static Scanner scan=new Scanner(System.in);
public static void main(String[] args)throws Exception
{
char ans='y';
String one, two;
int a, index;
two = "";
while(ans=='y') {
System.out.println("Enter a word and check wether it's a palindrome or not: ");
one = scan.nextLine();
index = one.length() - 1;
for(a = 0; a < one.length() ; a++){
two = two + one.charAt(index - a);
}
if(one.equalsIgnoreCase(two)){
System.out.println("The entered word is a palindrome.");
}else {
System.out.println("The entered word is not a palindrome. ");
}
System.out.println("Do you want to continue?");
ans=(char)System.in.read();
}
}
}
the output of this is wrong:
Enter a word and check wether it's a palindrome or not:
madam
The entered word is a palindrome.
Do you want to continue?
y
Enter a word and check wether it's a palindrome or not:
The entered word is not a palindrome.
Do you want to continue?
Enter a word and check wether it's a palindrome or not:
The entered word is not a palindrome.
Do you want to continue?
The first place, the output is correct but then when the user want to continue it will display this "Enter a word and check wether it's a palindrome or not:
The entered word is not a palindrome.
Do you want to continue?" again and again.
Thank you very much. I need your help pls..
- 02-18-2009, 03:08 AM #2
please use code tags...
change ans to type String and use ans=scan.nextLine(); instead of System.in.read();
also, set two=""; on everyloop.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-18-2009, 01:22 PM #3
Try changing the following:
toJava Code:one = scan.nextLine();
That will fix your problem.Java Code:one = scan.next();
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-18-2009, 01:45 PM #4
Interesting ....
You know Mika, I just have reviewed your post history for this year and you never have said thanks for the help you have received. That's not very nice for somebody who has come continiously to the forum for help.
Just a thought...
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-18-2009, 02:46 PM #5
Another way to do this
Just thought I would throw in my 2 cents worth...
The method that the OP is using is basically taking the original word, flipping it backwards and comparing it with the original word. This is Ok and acceptable. Another way to do this would be to compare the opposite ends of the letters in the word. For example...
The advantage of this is performance. A non-palidrome word would be detected a lot faster than taking the original word, flipping it and then comparing the two words.word -> abccba
place -> 123456
compare letter 1 vs letter 6, then
compare letter 2 vs letter 5, then
compare letter 3 vs letter 4
Just a though....
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
StringTokenizer in a Palindrome program
By jeremyk in forum New To JavaReplies: 10Last Post: 02-13-2010, 06:35 PM -
Palindrome Test
By Ada in forum New To JavaReplies: 1Last Post: 05-26-2007, 01:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks