# Java Palindrome Tester

Page 1 of 1

## 3 Replies - 5765 Views - Last Post: 07 October 2010 - 11:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=192860&amp;s=45bbf06e1a2979ed5a89ccb7f51782f6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 DirtyMartyr

• New D.I.C Head

Reputation: -2
• Posts: 12
• Joined: 30-September 10

# Java Palindrome Tester

Posted 30 September 2010 - 12:14 PM

The original assignment is as follows:

A number with the same digits from either end is called a palindrome. Examples are: 16261, 93439, and 20102. Write a program that accepts a 5-digit number from the user. Now, tell the user if the number s/he entered is a palindrome.

Your program should then proceed to print all the 5-digit palindrome numbers that are divisible by 17 and the sum of the digits of each number in a table as shown below. Also print the total number of 5-digit palindrome numbers that are divisible by 17.

Sample Run of the program:

Please enter a 5 digit number: 12321
You entered a palindrome

Table of 5 digit palindromes divisible by 17 and the sum of their digits:
Palindrome Sum of Digits

11611 10
12121 7
… …
… …
… …
98889 42
99399 39

There were a total of _?_ 5-digit Palindromes divisible by 17.

**************************************…
My original program:

```import javax.swing.JOptionPane;
public class Assignment2222
{
public static void main(String[] args)
{

char firstChar = '0';
char secondChar = '1';
char thirdChar = '2';
char fourthChar = '3';
char fifthChar = '4';
String numberInput;
int number;

numberInput = JOptionPane.showInputDialog("\nPlease enter a 5-digit number: ");
number = Integer.parseInt(numberInput);
firstChar = numberInput.charAt(0);
secondChar = numberInput.charAt(1);
thirdChar = numberInput.charAt(2);
fourthChar = numberInput.charAt(3);
fifthChar = numberInput.charAt(4);

if (firstChar == fifthChar && secondChar == fourthChar)
{
JOptionPane.showMessageDialog(null, "This is a Palindrome.");
}
else
{
JOptionPane.showMessageDialog(null, "Error: This is not a Palindrome.");
}
}
}

```

**************************************…
I realize there are several ways to do this. I posted the way I chose to do this w/ my basic, limited java skills. Since I'm really new to java, I decided to use char. I cant use Reverse method, or string arrays (string literals are ok). I have gotten the program to check if the user entered a Palindrome or not, but now I need to print out all 5 digit Palindromes divisible by 17 and the sum of the digits of each number. I think this is going to use increment, or decrement, but I have a lot of trouble using ++, -- properly. I realize its extremely important where you put these (pre, post). Can anybody please help me? Thanks in advance!!

Is This A Good Question/Topic? 0

## Replies To: Java Palindrome Tester

• D.I.C Regular

Reputation: 118
• Posts: 388
• Joined: 14-June 09

## Re: Java Palindrome Tester

Posted 30 September 2010 - 04:13 PM

About the ++(and --) operator, if you use them prefix(eg. ++x) or postfix(eg. x++), it won't matter if you use it to just add an expression on it's own.
So:
```int x = 5;
x++; //6
++x; //7

```

Things change when it is used as part of an expression, like :
```int x = 5;
int y;
y = ++x;//this does: a) add 1 to x, THEN make y the same as x...so after this x=y=6
y = x++;//this does: a) make y the same as x, THEN add 1 to x...so after this line y = 6, x = 7

```

About your problem, you could make a loop that loops from 10000 to 99999(all 5 digit nr's), then check if all conditions are met, and if so, display that number. Hope this will get you on your way, good luck!

### #3 pbl

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

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

## Re: Java Palindrome Tester

Posted 30 September 2010 - 04:20 PM

This would make more sense and is not number length dependant

```numberInput = JOptionPane.showInputDialog("\nPlease enter a  number: ");
number = Integer.parseInt(numberInput);

int reverse = 0;
int copy = number;
while(copy != 0) {
reverse *= 10;
reverse += copy % 10;
copy /= 10;
}
if(reverse == number) you have a palindrome

```

### #4 DirtyMartyr

• New D.I.C Head

Reputation: -2
• Posts: 12
• Joined: 30-September 10

## Re: Java Palindrome Tester

Posted 07 October 2010 - 11:17 PM

pbl, on 30 September 2010 - 03:20 PM, said:

This would make more sense and is not number length dependant

```numberInput = JOptionPane.showInputDialog("\nPlease enter a  number: ");
number = Integer.parseInt(numberInput);

int reverse = 0;
int copy = number;
while(copy != 0) {
reverse *= 10;
reverse += copy % 10;
copy /= 10;
}
if(reverse == number) you have a palindrome

```

Thanks for taking time out to read my post & for the helpful suggestion!!

Renagado, on 30 September 2010 - 03:13 PM, said:

About the ++(and --) operator, if you use them prefix(eg. ++x) or postfix(eg. x++), it won't matter if you use it to just add an expression on it's own.
So:
```int x = 5;
x++; //6
++x; //7

```

Things change when it is used as part of an expression, like :
```int x = 5;
int y;
y = ++x;//this does: a) add 1 to x, THEN make y the same as x...so after this x=y=6
y = x++;//this does: a) make y the same as x, THEN add 1 to x...so after this line y = 6, x = 7

```

About your problem, you could make a loop that loops from 10000 to 99999(all 5 digit nr's), then check if all conditions are met, and if so, display that number. Hope this will get you on your way, good luck!

I was thinking about doing something like you suggested for generating all 5 digit palindromes. Thanks for the help & the ++, -- explanation!