# 5 digit integer palindrome

Page 1 of 1

## 2 Replies - 17759 Views - Last Post: 14 March 2011 - 08:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=222161&amp;s=135a48c2ddbaada3ca0b64656e7eb5ab&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mistersinyster

Reputation: 1
• Posts: 10
• Joined: 14-March 11

# 5 digit integer palindrome

Posted 14 March 2011 - 07:49 PM

having trouble with the while loop. i'm supposed to check to make sure that the entered integer is actually a five digit number. it complies and runs fine except for the loop. thanks in advance for any help or advice...

```// Lab 2: Palindrome.java
// Program tests for a palindrome
import java.util.Scanner;

public class Palindrome
{
// checks if a 5-digit number is a palindrome
public void checkPalindrome()
{
Scanner input = new Scanner( System.in );

int number; // user input number
int digit1; // first digit
int digit2; // second digit
int digit3;
int digit4; // fourth digit
int digit5; // fifth digit
int digits; // number of digits in input

number = 0;
digits = 0;

/* Write code that inputs a five-digit number. Display an error message
if the number is not five digits. Loop until a valid input is received. */
System.out.println( "Enter five digit number: " ); // prompt
number = input.nextInt(); // read number
while (number < 10000 && number > 99999)
{
System.out.println( "Enter five digit number: " ); // prompt
number = input.nextInt(); // read number
}
/* Write code that separates the digits in the five digit number. Use
division to isolate the left-most digit in the number, use a remainder
calculation to remove that digit from the number. Then repeat this
process. */

digit1 = number / 10000;
digit2 = number / 1000 % 10;
digit3 = number % 1000 / 100 % 10;
digit4 = number % 10000 % 1000 % 100 / 10;
digit5 = number % 10000 % 1000 % 100 % 10;
/* Write code that determines whether the first and last digits are
identical and the second and Fourth digits are identical. Output
whether or not the original string is a palindrome. */
if (digit1 == digit5 && digit2 == digit4)
System.out.printf("%d is a palindrome", number);
else
System.out.printf("%d is not a palindrome", number);

} // end method checkPalindrome
} // end class Palindrome

```

Is This A Good Question/Topic? 0

## Replies To: 5 digit integer palindrome

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: 5 digit integer palindrome

Posted 14 March 2011 - 08:12 PM

if(number < 10000 || number > 99999)
... invalid input

not &&

### #3 mistersinyster

Reputation: 1
• Posts: 10
• Joined: 14-March 11

## Re: 5 digit integer palindrome

Posted 14 March 2011 - 08:21 PM

its always something simple... thanks