Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,165 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,666 people online right now. Registration is fast and FREE... Join Now!




Creating a 5 digit palindrome

 
Reply to this topicStart new topic

Creating a 5 digit palindrome

fletchluv
post 10 Oct, 2008 - 09:24 PM
Post #1


New D.I.C Head

*
Joined: 3 Oct, 2008
Posts: 13


My Contributions


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.
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.gif
Thanks - BetaWar
User is online!Profile CardPM

Go to the top of the page

markhazlett9
post 10 Oct, 2008 - 09:28 PM
Post #2


D.I.C Head

**
Joined: 12 Jul, 2008
Posts: 133



Thanked 8 times
My Contributions


Is a Palindrone not a word or string just backwards? How will you be able to tell if a number is backwards? Just curious
User is online!Profile CardPM

Go to the top of the page

fletchluv
post 10 Oct, 2008 - 09:45 PM
Post #3


New D.I.C Head

*
Joined: 3 Oct, 2008
Posts: 13


My Contributions




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.


User is online!Profile CardPM

Go to the top of the page

Gloin
post 11 Oct, 2008 - 07:17 AM
Post #4


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 02:36PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month