Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Check for palindrome numbers

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Check for palindrome numbers

    First of, hi I'm a new user here. Learning my first year of Java.
    This is just an exam question which I couldn't solve but am eager to find the answer to.

    int num[] = {121, 13, 34, 11, 22, 54};

    Using the for loop and while loop
    The user is to check for palindrome numbers within the array and output it as follows:
    121 is a palindrome number
    13 is a not palindrome number
    34 is a not palindrome number
    11 is a palindrome number
    22 is a palindrome number
    54 is a not palindrome number

    Previous I was taught how to print a given integer in reverse

    int num = 123456789
    while (num > 0) {
    System.out.print(num % 10);
    num /= 10;
    }
    This allows me to print out the integer in reverse. But in this question I'm required to somehow save the integer generated from the while loop and compare it to the array.
    Here's what I did:
    int num[] = {121, 13, 34, 11, 22, 54};
    String check[] = new String[6];
    String temp;
    for (int i = 0; i < num.length; i++) {
    while (num.[i] > 0) {
    temp = Integer.toString(num[i] % 10);
    check[i] = check[i] + temp;
    num[i] /= 10;
    } // end while loop
    if (check[i] = Integer.toString(num[i]) {
    System.out.println(num[i] + " is a palindrome number")
    } else {
    System.out.println(num[i] + " is not a palindrome number")
    } // end for loop
    Seems like using Integer.toString causes all my num[i] to become 0.

    P.S. I'm not sure if this is the right section to be asking this. If it's in the wrong section, would a Mod move it to the right one please.
    Last edited by SnooSnoo; February 19th, 2010 at 08:22 AM.


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: Check for palindrome numbers

    I can't really follow your code. Take a look at the following and see if it's similar or different from the way I would do it:

    ListPalindroms(numbers[1...n] : integer)
    1. for i := 1 to n
    2.    do if IsPalindromic(toString(numbers[i])) then print(numbers[i], " is palindromic.")
    3.    else then print(numbers[i], " is not palindromic.")
     
    IsPalindromic(word : string)
    1. if len(word) < 2 then return true
    2. else if word[1] != word[len(word)] then return false
    3. else return IsPalindromic(word[2...len(word)-1])
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Check for palindrome numbers

    Here is the code that would find out the palindrome number. You need not take another String array if you don't want to use it outside the loop.


    int num[] = {121, 13, 34, 11, 22, 54};
            String check = "";
            String temp="";
     
            for(int i=0; i<num.length;i++){
                check=Integer.toString(num[i]);
                while(num[i]>0){
                    temp=temp+Integer.toString(num[i]%10);
                    num[i]=num[i]/10;
                }
     
                if(check.equals(temp)){
                    System.out.println(num[i] + "is a palindrome number");
                }else{
                    System.out.println(num[i] + "is not a palindrome number");
                }
                temp="";
                check="";
            }

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Check for palindrome numbers

    Quote Originally Posted by vivekbansal View Post
    Here is the code that would find out the palindrome number. You need not take another String array if you don't want to use it outside the loop.



    int num[] = {121, 13, 34, 11, 22, 54};
    String check = "";
    String temp="";

    for(int i=0; i<num.length;i++){
    check=Integer.toString(num[i]);
    while(num[i]>0){
    temp=temp+Integer.toString(num[i]%10);
    num[i]=num[i]/10;
    }

    if(check.equals(temp)){
    System.out.println(num[i] + "is a palindrome number");
    }else{
    System.out.println(num[i] + "is not a palindrome number");
    }
    temp="";
    check="";
    }
    I run the program and end up getting
    0 is not a palindrome number
    0 is not a palindrome number
    0 is not a palindrome number
    0 is not a palindrome number
    0 is not a palindrome number
    0 is not a palindrome number
    I've no idea why the array values become 0. I added a system.out.println OUTSIDE of the for loop and it successfully prints the value.
    Quote Originally Posted by CT&SC View Post
    I can't really follow your code. Take a look at the following and see if it's similar or different from the way I would do it:

    ListPalindroms(numbers[1...n] : integer)
    1. for i := 1 to n
    2.    do if IsPalindromic(toString(numbers[i])) then print(numbers[i], " is palindromic.")
    3.    else then print(numbers[i], " is not palindromic.")
     
    IsPalindromic(word : string)
    1. if len(word) < 2 then return true
    2. else if word[1] != word[len(word)] then return false
    3. else return IsPalindromic(word[2...len(word)-1])
    Completely Missing The Point

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Check for palindrome numbers

    Are you sure that is the output you are getting? I pasted your code in directly, and the output I see is:

    0 is a palindrome number
    0 is not a palindrome number
    0 is not a palindrome number
    0 is a palindrome number
    0 is a palindrome number
    0 is not a palindrome number

    So the results are correct, but you are actually modifying the integers, which is why they are printing out as 0. Take another look at your loop:

    while(num[i]>0){
      temp=temp+Integer.toString(num[i]%10);
      num[i]=num[i]/10;
    }

  6. #6
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Check for palindrome numbers

    Quote Originally Posted by jasonm View Post
    Are you sure that is the output you are getting? I pasted your code in directly, and the output I see is:

    0 is a palindrome number
    0 is not a palindrome number
    0 is not a palindrome number
    0 is a palindrome number
    0 is a palindrome number
    0 is not a palindrome number

    So the results are correct, but you are actually modifying the integers, which is why they are printing out as 0. Take another look at your loop:

    while(num[i]>0){
      temp=temp+Integer.toString(num[i]%10);
      num[i]=num[i]/10;
    }
    Ah yes I see now. I just have to create another array holding the same values and use that when printing.
    I didn't realize the /10 part until you pointed it out. ( _
    The original printReverse didn't require the integers to be used in the printing so I didn't think of it.
    Thanks!

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Check for palindrome numbers

    Like the name SnooSnow lol Reminds me of Futurama
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  8. #8
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Check for palindrome numbers

    Yes, it was getting reset to 0, I should have used variable check, where I am storing the value to print.

    Here is the one which will return the result with values.

    	int num[] = {121, 13, 34, 11, 22, 54};
    		String check = "";
    		String temp="";
     
     
    		for(int i=0; i<num.length;i++){
    			check=Integer.toString(num[i]);
    			while(num[i]>0){
    				temp=temp+Integer.toString(num[i]%10);
    				num[i]=num[i]/10;
    			}
     
    			if(check.equals(temp)){
    				System.out.println(check + "is a palindrome number");
    			}else{
    				System.out.println(check + "is not a palindrome number");
    			}
     
    			temp="";
    			check="";
    		}
    Last edited by helloworld922; March 4th, 2010 at 10:43 PM. Reason: please use [code] tags

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  2. help me .. about creating random numbers
    By soldier in forum Java Theory & Questions
    Replies: 2
    Last Post: December 20th, 2009, 07:24 PM
  3. Roman Numbers
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 12th, 2009, 01:19 AM
  4. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM
  5. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM