Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Question Palindrome question

    Hi,
    I am working on a class that has to decide whether given number is palindrome or not. I need to consider two cases - when input is integer, but defined as a String and when input is defined as a number.
    I am done with the first case - when the input is considered as a String.
    Regarding the numerical input I have no idea where to start from. I want to try do it myself first before asking you, guys for advice.
    So about the input, interpreted as a String.

    Java Code:
    public static void main(String[] args) {
            
            
            Scanner input = new Scanner(System.in);
            System.out.print("Enter number: ");
            int num = input.nextInt();
            
            String str = String.valueOf(num);
                    
            StringBuilder sb1 = new StringBuilder(str);
            StringBuilder sb2 = sb1.reverse();
                    
            if( sb2.toString().equals(str) )
                System.out.print("\nPalindrome");
            else
                System.out.print("Not palindrome");
    This code is fine.

    But when I change the if to

    Java Code:
    if( sb2.equals(sb1))
                System.out.print("\nPalindrome");
    it always gives me that the number is a palindrome even if it is not.
    Why is that?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    The StringBuilder.reverse() method returns the characters in the buffer/builder itself and it also returns the same StringBuilder; you could've written this instead:

    Java Code:
    StringBuilder sb1 = new StringBuilder(str);
    sb1.reverse();
    StringBuilder sb2 = sb1;
    The last line of the code now clearly shows that both sb1 and sb2 refer to the same StringBuilder object (so they're definitely equal).

    kind regards,

    Jos

  3. #3
    xacml's Avatar
    xacml is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

    StringBuilder java.lang.StringBuilder.reverse()
    Reverses the order of characters in this StringBuilder.

    Returns:
    this StringBuilder
    Because; sb1 & sb2 are same. Reverse method doesn't create and return a new instance of StringBuilder.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Regarding the numerical input I have no idea where to start from
    First you need to decide how the number is to be represented. In binary bit by bit,
    or as a String in some base, normally 10 but could be any base.

  5. #5
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Thanks to everyone for the replies.I have been very busy today and didn't really have time to check out what you have said.Hopefully I will have more time tomorrow.

    Luke

  6. #6
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    First you need to decide how the number is to be represented. In binary bit by bit,
    or as a String in some base, normally 10 but could be any base.
    When you say "as a String in some base, normally 10" you mean whether I am going to use % 10 or another base?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    I was asking how you represent the number. The number fifteen is 15 or 017 or 0x0F or 00001111 depending on the base.

  8. #8
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    I was asking how you represent the number. The number fifteen is 15 or 017 or 0x0F or 00001111 depending on the base.
    You were asking me if it was going to be decimal, hexadecimal etc. number. I am sorry, I didn't understand you, Norm.
    It is a decimal number.

  9. #9
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Based on my research I noticed the first step in resolving that task is to have input_number % 10.
    And I guess I have to do
    Java Code:
    for( int i = 0; i <= input_number; i++ )
    But that's all I can think of... Any hints?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by luke View Post
    Based on my research I noticed the first step in resolving that task is to have input_number % 10.
    And I guess I have to do
    Java Code:
    for( int i = 0; i <= input_number; i++ )
    But that's all I can think of... Any hints?
    Indeed, when you n%10 a number n you get the rightmost digit of that number; n/10 removes the rightmost digit from that number. btw doing r*10+d adds a digit d to a number r (<--- hint hint).

    kind regards,

    Jos

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Why not convert the number to a String and treat it like any other String vs doing the arithmetic stuff?

  12. #12
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Why not convert the number to a String and treat it like any other String vs doing the arithmetic stuff?
    I already did it. Now I want to do it when the input is a decimal number.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Explain the difference between a String of letters and a String of digits?
    Or is it part of the assignment to work with int values vs Strings?
    A palindrome doesn't make sense to me unless you are talking about a String.
    Unless you are at the bit level or byte level.

  14. #14
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Indeed, when you n%10 a number n you get the rightmost digit of that number; n/10 removes the rightmost digit from that number. btw doing r*10+d adds a digit d to a number r (<--- hint hint).

    kind regards,

    Jos
    Java Code:
    System.out.print("Enter number: ");
    int input_number = input.nextInt();
            
    int reversed = 0, n = input_number;
            
    for( int i=0; i <= input_number; i++ ) {
          int remainder = input_number % 10;
          input_number = input_number / 10;
          reversed = reversed * 10 + remainder ;
    [B][COLOR="Red"] i = 0;[/COLOR][/B]
    }
    Why the code doesn't work correctly when I remove i = 0; ?
    Also when I enter 00121 (for example) my program tells me it's a palindrome, while it is not.
    Last edited by luke; 09-30-2010 at 06:39 PM.

  15. #15
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Explain the difference between a String of letters and a String of digits?
    Or is it part of the assignment to work with int values vs Strings?
    A palindrome doesn't make sense to me unless you are talking about a String.
    Unless you are at the bit level or byte level.
    There is no difference. I just want to do it with integer values because it's a different approach. And I've found it more interesting than the String representation.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Why the code doesn't work correctly
    Try debugging the code by printing out the values of ALL the variables used as they are changing in the loop.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by luke View Post
    Java Code:
    System.out.print("Enter number: ");
    int input_number = input.nextInt();
            
    int reversed = 0, n = input_number;
            
    for( int i=0; i <= input_number; i++ ) {
          int remainder = input_number % 10;
          input_number = input_number / 10;
          reversed = reversed * 10 + remainder ;
    [B][COLOR="Red"] i = 0;[/COLOR][/B]
    }
    Why the code doesn't work correctly when I remove i = 0; ?
    Also when I enter 00121 (for example) my program tells me it's a palindrome, while it is not.
    Because your algoritm doesn't make any sense. Resetting the loop counter i is suspicious to say the least.

    kind regards,

    Jos

  18. #18
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Try debugging the code by printing out the values of ALL the variables used as they are changing in the loop.
    Do you know how I can do step execution in Eclipse? I am printing out the variables during execution but when I press Enter after entering the input number it executes the program all at once.

    Edit: I've found it but for some reason it is not active.

    Edit2: I've found it.
    Last edited by luke; 09-30-2010 at 11:29 PM.

  19. #19
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Because your algoritm doesn't make any sense. Resetting the loop counter i is suspicious to say the least.

    kind regards,

    Jos
    Actually this is not my algorithm. I found it somewhere in the Web. I tried to find the algorithm but while I was looking for it I found this code. I thought if I understand what the code does I would understand how the algorithm works.

  20. #20
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Because your algoritm doesn't make any sense. Resetting the loop counter i is suspicious to say the least.

    kind regards,

    Jos
    To me this i = 0; should not be there. I did step execution and the counter just gets crazy values.
    Anyway, I've found a way. Thanks, guys.

Page 1 of 2 12 LastLast

Similar Threads

  1. Test for Palindrome
    By arson09 in forum New To Java
    Replies: 11
    Last Post: 09-29-2010, 07:57 PM
  2. Palindrome
    By pinkdreammsss in forum Java Applets
    Replies: 8
    Last Post: 05-04-2010, 03:59 PM
  3. HELP...Palindrome
    By d7o0om in forum New To Java
    Replies: 12
    Last Post: 11-13-2009, 03:32 AM
  4. palindrome problem
    By Blaedel in forum New To Java
    Replies: 5
    Last Post: 10-03-2009, 12:52 AM
  5. Palindrome Test
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:36 PM

Posting Permissions

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