3 Replies - 3731 Views - Last Post: 11 October 2008 - 08:17 AM Rate Topic: -----

#1 fletchluv  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 03-October 08

Creating a 5 digit palindrome

Posted 10 October 2008 - 10:24 PM

In my Java class I must create a program that takes a 5 digit number from keyboard input and tells you if the number that was inputted is a palindrome or not. I am very close to making it work, but I am having trouble deriving the 5th digit. I am thinking that is why the program will not calculate if the value is a palindrome or not because it cannot account for the 5th digit. When I use a 4 digit number it works fine. The code does compile correctly. I just need a little guidance to ensure I am on the right path. Here is my code.
											  import java.util.*;
											  class palindrome
											  {
											  public static void main(String[] args)
											  {
											   int n,n1,n2,n3,digit1,digit2,digit3,digit4,digit5;
											   Scanner keyboard = new Scanner(System.in);
											   System.out.println("Input a 5 digit number:");
											   n = keyboard.nextInt();
											   digit1 = n/1000;
											   n1 = n % 1000;
											   digit2 = n1 / 100;
											   n2 = n1 % 100;
											   digit3 = n2 / 10;
											   digit4 = n2 % 10;
											   n3 = n2 / 1;
											   digit5 = n3 % 1;
											   String total = String.valueOf(digit1)+String.valueOf(digit2)
											   +String.valueOf(digit3)+String.valueOf(digit4)+String.valueOf(digit5);
											   int sum = Integer.parseInt(total);
											   System.out.println("The number is " + sum);
											   if((digit1 == digit5) || (digit5 == digit1))
											   {
											   System.out.println("Number is a Palindrome");
											   }
											   else 
											   {
											   System.out.println("Number is not a Palindrome");
											  }
											  }
											  }									   


Any guidance I can get would be great. Thank you for your time.

Mod Edit - Please :code:
Thanks - BetaWar

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a 5 digit palindrome

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Creating a 5 digit palindrome

Posted 10 October 2008 - 10:28 PM

Is a Palindrone not a word or string just backwards? How will you be able to tell if a number is backwards? Just curious
Was This Post Helpful? 0
  • +
  • -

#3 fletchluv  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 03-October 08

Re: Creating a 5 digit palindrome

Posted 10 October 2008 - 10:45 PM

Forgive me since I am brand new to Java, from what my instructor had mentioned this is what he said a Palindrone should be:

" Write a program that inputs a 5-digit integer from the keyboard and prints if the
input number is a palindrome or not.
A palindrome is a number, which has the same digit from either end.
Examples:
12921 is a palindrome.
12923 is not a palindrome "

I just created the program to be able to perform that function as he mentioned. Hope this clears it up.
Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Creating a 5 digit palindrome

Posted 11 October 2008 - 08:17 AM

Instead of reading the input as integer, try reading it as a string.
Then all you need to do is using the String.charAt(int i) method.

Check that

String.charAt(0) = String.charAt(4)
and
String.charAt(1) = String.charAt(3)

voila!, problem solved
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1