3 Replies - 4391 Views - Last Post: 07 October 2010 - 11:17 PM Rate Topic: -----

#1 DirtyMartyr  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • 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

#2 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • 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!
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,910
  • 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


Was This Post Helpful? 1
  • +
  • -

#4 DirtyMartyr  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 12
  • Joined: 30-September 10

Re: Java Palindrome Tester

Posted 07 October 2010 - 11:17 PM

View Postpbl, 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!!

View PostRenagado, 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1