To sumitlole.
NO, it is not the easiest way, it is the wrong way. This thread is 2.5 years old. What do you think you can accomplish.
In your code you have this:
System.out.println("Wrong Number Enter again");
if(s.charAt(0)==s.charAt(4) && s.charAt(1)==s.charAt(3))
You print the message "try again" but then you don't have a while loop for the user to enter again a value until the right one is entered. No matter the size of the input this code will be executed: if(s.charAt(0)==s.charAt(4) && s.charAt(1)==s.charAt(3)) which result to your program to crash.
Not to mention other little mistakes that make your code not safe to run
About your edit:
Use code tags !! Your code is difficult to read with all these colors
javaAddict
Nearly a Senior Poster
3,338 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 450
Skill Endorsements: 7
If all u need is a code snippet for checking whether a given word is a palindrome, then use the one below:
I found interesting that code but noticed a few things that I didn't like:
while(left<right)
{
if(word.charAt(left)!=word.charAt(right))
{
flag=0;
}
left++;
right--;
[B]if(flag==1)
{
System.out.println("The word" + word + "is a palindrome");
}
else
{
System.out.println("The word"+ word + "is not a palindrome");
}[/B]
}
The bolded part shouldn't be in the loop.
Take this input: "abcdefgcba"
Because for the first checks it will print that it is palindrome, then it will keep printing that is not for a few loops before exiting.
You should print the result outside the loop, only once.
Also when the flag is set to 0 you can break from the loop because it is not necessary to continue checking. You found that it is not a palindrome so no need to check the rest of the word
javaAddict
Nearly a Senior Poster
3,338 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 450
Skill Endorsements: 7
Create an application that reads in a five-digit integers and determines whether it is a palindrome.If the number is not five digits long,display an error messsage and allow the user to enter a new value.
Read the forum's rules. Also, read whatever has been written in this thread. Since you found it, you should know that it answers your question.
javaAddict
Nearly a Senior Poster
3,338 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 450
Skill Endorsements: 7
a java application that asks an INTEGER number from the user.Then output each digit in a newline.
Haven't you read my previous reply? Your initial question has been answered in this thread. For new questions start an new thread. What do you think this is? A thread were you can throw your homework and disregard the forum rules? If you want help start a new thread and read the forum rules, which you agreed when you signed in.
javaAddict
Nearly a Senior Poster
3,338 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 450
Skill Endorsements: 7
The more simplest one is
public class Palindrome {
static public String pal(String str, int i, int j){
if(str.length()/2 != i)
if(str.charAt(i)==str.charAt(j))
pal(str,++i,--j);
else
return "String is Not palaindrome";
return "String is palaindrome";
}
public static void main(String[] args) {
String str = args[0];
System.out.println(pal(str,0,str.length()-1));
}
}
an even easier way would have been to read the dates of the last posts, realise it is an ancient, dead thread and either already answered, or the OP is no longer following this thread.
this thread may not yet have been marked as 'closed' but if you bothered to look at the dates of the posts, you should have realised it should be treated as if it were.
stultuske
Industrious Poster
4,489 posts since Jan 2007
Reputation Points: 1,377
Solved Threads: 627
Skill Endorsements: 25