0

anyone helps me figure out what is wrong with my isPalindrome(int) function?

Basically this function checks if a number is a palindrome, and I wanted to accomplish this by recursion. Some problem occurs when isPalindrome(int) is called within the function. This brought my a lot of headache. Thanks!

public boolean isPalindrome(int num) {
    String s = Integer.toString(num);
    if( s.length() == 1 ) {
    return true;
}
if( s.length() == 2 && s.charAt(0) == s.charAt(1) ) {
    return true;
}
if( s.length() > 2 ) {
    if(s.charAt(0) == s.charAt(s.length()-1))
        s = s.substring(1, s.length()-1);
        **isPalindrome(Integer.parseInt(s));**
}
return false;
}
4
  • We usually don't do your homework for you. Commented Jan 17, 2012 at 16:57
  • 1
    You will also have problems if there are 0s in your int. Transform you int into a String, and then use an isPalindrome method that works only with Strings and doesn't convert to int anymore. Commented Jan 17, 2012 at 17:00
  • 2
    Best to say what the "Some problem" is too. Commented Jan 17, 2012 at 17:04
  • Your question is almost a duplicate. Check this stack overflow post, write your code. Commented Jan 17, 2012 at 17:37

5 Answers 5

2

In this part of your code

       if(s.charAt(0) == s.charAt(s.length()-1))
         s = s.substring(1, s.length()-1);
        **isPalindrome(Integer.parseInt(s));**

You have not given else for the condition when first and last characters are not equal. You should return false when they are not equal. And also 'return' isPalindrome(Integer.parseInt(s)), else the last return will be executed after execution of the function.

     if(s.charAt(0) == s.charAt(s.length()-1)) {
         s = s.substring(1, s.length()-2);
         return isPalindrome(Integer.parseInt(s));
     } else {
       false;
     }
Sign up to request clarification or add additional context in comments.

Comments

1

you should return isPalindrome(Integer.parseInt(s)); and not just invoke it.

If you don't do it, when you come back from the recursion, you quit the last if's scope, and return false, no matter what the recursive call returned.

Comments

0

Sounds like [homework] You would be able to see the problem by stepping through your code in a debugger, but one problem you have is that the value returned by isPalindrome is ignored.

Another problem you have is that 0 in the first half of the number are ignored. This is because you are converting the string to an int and back into a String so 1020321 would appear to be a palindrome.

BTW: This question has been asked many times before. Have you compared your answer with others on the web?

Comments

0
public class Recursion {
    public static String Palindrome(int length)
        {
        //Alphabet to pick letters from
        String alpha="abcdefghijklmnopqrstuvwxyz";
        String s="";
        String sBackwards=" ";
        if(length==0) {
            sBackwards+=reverse(s);
            return sBackwards;
            //recursion over-rides sBackwards
        } else {
            Random r = new Random();
            //places a char from alpha into s
            s+=(alpha.charAt((int)r.nextInt(26)));
            System.out.print(s);
            return Palindrome(length-1); //recursion
        }
    }
    public static void main(String args[])
    {
        System.out.println(Palindrome(10));
    }
}

Comments

0

You want s.Length()-2, not s.Length()-1

Also you can change your first true test to <= 1, and remove the special case of length == 2.

2 Comments

This is wrong. using length()-2 will produce a string which ends too soon. think what it will do on input "aaa". [hint: "aaa".substring(1,"aaa".length()-2) results in empty string]
Sorry - was thinking in .NET where substring takes start index and length!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.