Search code examples
javadebuggingrecursionpalindrome

Palindrome Test: Debugging


I'm having trouble figuring out the bug(s) in my code. I know there is one except I'm not sure what it is. I am new to coding and this is only my second semester, my professor had us skip to the end of the book to learn recursion however in the labs the book assigns the author wants us to implement other skills which would be learned in chapters we haven't gone through yet. In other words I'm using things I haven't really learned yet so I'm not seeing the mistake here. Anyone who can figure out what I did wrong and explain it too me your assistance would be greatly appreciated! Thanks!

    if (palTest(s.toLowerCase().replaceAll("\\W","")))
        System.out.println("\nYour phrase is a palindrome!");

    public static boolean palTest(String str)
    {

        char first = str.charAt(0);
        char last = str.charAt(str.length() - 1);


        System.out.println(str);
        System.out.println(first + " --- " + last);
        System.out.println("----------------\n");


        if (str.length() <= 2)
            return true;
        else if (first != last)
            return false;
        else
            return palTest(str.substring(1, str.length()-1));
    }

The if statement is my call to the palTest method. Now this code works for almost all of the phrases I've entered. However, when I enter this phrase: "Dessertsm I stressed" it will compare the letter m to the letter i last and return true and say that the phrase IS a palindrome.

I put some System.out.println statements in so I could see exactly what's happening, here is what it looks like when it prints out.

Please enter a phrase you wish to test to discover if it it a palindrome: 
dessertsm i stressed

dessertsmistressed
d --- d
----------------

essertsmistresse
e --- e
----------------

ssertsmistress
s --- s
----------------

sertsmistres
s --- s
----------------

ertsmistre
e --- e
----------------

rtsmistr
r --- r
----------------

tsmist
t --- t
----------------

smis
s --- s
----------------

mi
m --- i
----------------


Your phrase is a palindrome!

The letter m can be replaced with anything and it returns the same. Please help! Thank you!


Solution

  • It's because you stop the recursion if the length is 2. Wait until it's less than 2 to stop. So change this:

    if (str.length() <= 2)
        return true;
    

    to this:

    if (str.length() < 2)
        return true;
    

    The problem then is that it will crash it in the earlier checks, so move it to the start of the method. So basically, any string of length < 2 is trivially a palindrome, and you don't need to do any checks on it; just return true. It's only for longer strings that you need to compare the first and last character. Your final code should look like this:

    public static boolean palTest(String str)
    {
        if (str.length() < 2) return true;
    
        char first = str.charAt(0);
        char last = str.charAt(str.length() - 1);
    
        if (first != last){
            return false;
        } else{
            return palTest(str.substring(1, str.length()-1));
        }
    }