how to write an application that reads in a five digit integers and determine whether the number is as palindromes or not e.g 12321,11611.If the number is not five digit long, display an error dialog, allow the user to enter a new value.

Dani AI

Generated

A practical, robust plan is to validate input first (exactly five decimal digits), then test for palindrome. Two common, safe strategies are (1) treat the entry as a string: confirm length and that every character is a digit, then compare the string to its reverse; or (2) treat it as an integer: parse safely and check that the absolute value lies between 10000 and 99999, then reverse the digits numerically and compare. Either approach must run inside a loop that shows an error dialog on invalid input and re-prompts until a valid five-digit entry is supplied.

Validation details to keep in mind: trim whitespace, reject non-digit characters, and decide whether leading zeros or a leading minus sign are acceptable. If leading zeros should not be allowed, require the first character not be '0' when using the string method; if negative values are not allowed, require a positive range when using numeric checks. Always catch parsing exceptions (NumberFormatException) rather than assuming input will parse cleanly.

Palindrome checking notes: numeric reversal works well for five-digit numbers (extract last digit with modulus, build reversed number, compare), while the string method is simpler to implement and easier to extend. For word palindromes (as in ’s later post), normalize by removing non-alphanumeric characters and folding case before comparing; that handles phrases like "A man, a plan..." correctly.

The example posted by @bhi provides useful structure but needs explicit input parsing, error handling, and a clear user-facing error dialog when the length/range check fails. ’s suggestion to draft pseudocode first is good practice: a short pseudocode loop that validates input, reports errors, and then applies the palindrome test prevents common student mistakes. Useful test cases: 12321 (palindrome), 01210 (leading-zero case), -12321 (negative), 10000 (lowest valid five-digit), 100001 (too long).

Recommended Answers

All 7 Replies

With a text editor, of course.

Why don't you write a bit of pseudocode that outlines the general process the program must follow to complete the needed steps and post it here if you need some help with portions of it?

Try this. It can be made a lot more efficient. Next time please put at least a psuedo code.

import java.io.*;
class Pal
{
public static void main(String args[]) throws IOException
{
int num,rev=0,temp;
boolean flag=true;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(flag)
{
System.out.println("Enter a number:");
num=br.readline();
//To check if it is a 5 digit number
if(num/10000>=1 && num/10000<10)
flag=false;
}
temp=num;
//Reversing the number
while(num!=0)
{
rev=rev*10+num%10;
num=num/10;
}
if(rev==temp)
System.out.println("It is a palindrome");
else
System.out.println("Not palindrome");
}
}
commented: Don't just hand out homework anwers. -2

Also put an error message in the if block where u check for 5 digit numbers

Try this. It can be made a lot more efficient. Next time please put at least a psuedo code.

Handing students who have shown no effort at all on their assignment the answer is against the spirit of this forum, as evidenced by this prominent post at the top of the forum: http://www.daniweb.com/forums/announcement9-2.html

You are rewarding laziness and fostering incompetence.

I have to determine whether the word enter is a palindrome or not

That's a statement, not a question. Start a new thread if you have a specific question about the code that you have written so far.

If you haven't written any code yet then you haven't really put any effort into the assignment and probably shouldn't expect anyone to else to expend any time on it either.

given the already provided answer, did he even read the rest of the thread?
Does he even know what a palindrome is?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.