Results 1 to 6 of 6
  1. #1
    Dai
    Dai is offline Member
    Join Date
    Sep 2013
    Posts
    15
    Rep Power
    0

    Default Palindrome Program Java

    Hello, so here is my Palindrome code, I'm having problems with my loop, the program just print "Enter five digit number" and then skip everything until "Nope! This example is written only 5 digit number", any idea what am I doing wrong?, thank you in advance.

    import java.util.Scanner;

    public class Palindrome {
    public static void main (String [] args) {
    Scanner keyboard = new Scanner (System.in);
    int number = 0;
    int counter = 1;

    while (counter <=5) {
    System.out.printf ("\nEnter five digit number: \n");
    number = keyboard.nextInt();
    if(number <= 99999 && number > 99999) {
    if (palindrome (number))
    System.out.printf ("The number %d is a Palindrome! "+ number);
    else
    System.out.printf ("The number %d is NOT a Palindrome! "+ number);
    }
    else {// else statement
    System.out.printf ("\n\nNope! This example is written only 5 digit number. \n");
    System.out.printf ("Please try again and enter a 5 digit number. \n\n");
    counter--;
    }
    counter++;
    } //end while loop

    } //end main method
    public static boolean palindrome (int number) {
    int tenThou = 0, thou = 0, tens = 0, ones = 0;
    tenThou = number / 10000;
    thou = number % 10000 / 1000;
    tens = number % 100 / 10;
    ones = number % 10;
    if (tenThou == ones && thou == tens) // if this expression result true
    return true; // method return true
    else
    return false; // method return false
    } // end method palindrome

    }// end class

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: Palindrome Program Java

    Do you believe the following will ever be true?
    Java Code:
    if(number <= 99999 && number > 99999) {
    Regards,
    JIm
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Dai
    Dai is offline Member
    Join Date
    Sep 2013
    Posts
    15
    Rep Power
    0

    Default Re: Palindrome Program Java

    sorry Im new in Java, what do you mean with "ever be true?"

  4. #4
    Dai
    Dai is offline Member
    Join Date
    Sep 2013
    Posts
    15
    Rep Power
    0

    Default Re: Palindrome Program Java

    I mean the the program doesnt execute the loop because the value is too high?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,032
    Rep Power
    6

    Default Re: Palindrome Program Java

    It's a logic error. A value can't be both <= to a some number and > than that number at the same time. It's like saying you have a red hat on and a green hat on. They can't both be true even if you have either a red hat on or a green hat on. So you need to think about what you want to check and make certain it makes sense. Remember! && means both and || means one or the other.

    And please place you code between code tags [code][/code]. It makes it easier to read. I have done it for you here.

    Java Code:
    import java.util.Scanner;
     
    public class Palindrome {
        public static void main (String [] args) {
            Scanner keyboard = new Scanner (System.in);
            int number = 0;
            int counter = 1;
     
                while (counter <=5) {
                System.out.printf ("\nEnter five digit number: \n");
                number = keyboard.nextInt();
                if(number <= 99999 && number > 99999) {
                           if (palindrome (number))
                               System.out.printf ("The number %d is a Palindrome! "+ number);
                           else
                            System.out.printf ("The number %d is NOT a Palindrome! "+ number);
                }
                else {// else statement
                  System.out.printf ("\n\nNope! This example is written only 5 digit number. \n");
                  System.out.printf ("Please try again and enter a 5 digit number. \n\n");
                  counter--;
                  }
                  counter++;
                  } //end while loop
     
    } //end main method
              public static boolean palindrome (int number) {
             int tenThou = 0, thou = 0, tens = 0, ones = 0;
                 tenThou = number / 10000;
                 thou = number % 10000 / 1000;
                 tens = number % 100 / 10;
                 ones = number % 10;
       if  (tenThou == ones && thou == tens) // if this expression result true
             return true; // method return true
           else
             return false; // method return false
          } // end method palindrome
     
    }// end class
    Regards,
    Jim
    Last edited by jim829; 09-11-2013 at 12:17 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Dai
    Dai is offline Member
    Join Date
    Sep 2013
    Posts
    15
    Rep Power
    0

    Default Re: Palindrome Program Java

    Thank you so much ;) I will definitely tags next time ;) I been working in this code for the last 48 hrs :S

Similar Threads

  1. Palindrome Test Program
    By Zelaine in forum New To Java
    Replies: 5
    Last Post: 08-27-2013, 04:22 AM
  2. Palindrome Program
    By e.cummins95 in forum New To Java
    Replies: 7
    Last Post: 03-09-2012, 01:59 AM
  3. Palindrome Prime program
    By Zora in forum New To Java
    Replies: 7
    Last Post: 10-19-2011, 09:42 PM
  4. Palindrome program
    By trinity in forum New To Java
    Replies: 4
    Last Post: 04-16-2011, 04:22 AM
  5. Palindrome program help
    By Nel in forum New To Java
    Replies: 1
    Last Post: 03-03-2011, 04:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •