-1

I am wiriting a palindrome-checking boolean function in Java - I am learning recursion. When the word is stripped to the final (inner) one or two letters and the program should check if the two letters are the same it seems to not work - function always returns false. It seems odd that when program comes to return, it just continues to work - I know this because it continues to print my checkpoints (print statements). The print function is executed, but the return statement right after is ignored. And even when the function should return true (=the word is a palindrome), at the end it still returns false.

I have already tried combinations of if, if-else statements, different positions of return statements, the function isPalindrome is working correctly with the main function and I have tried lots of debugging with print functions.

My code:

public static boolean isPalindrome(String word) {
    int wlength = word.length();
    System.out.printf("wlength = %d%n", wlength);
    
    if (wlength == 1) {
        System.out.printf("wlength == 1!!!!%n");
        return true;

    } else if (wlength == 2) {
        System.out.printf("wlength == 2%n");
        String first = word.substring(0, 1);
        String last = word.substring(1, 2);
        if (first == last) {
            return true;
        } else {
            return false;
        }
    }

    String first = word.substring(0, 1);
    String last = word.substring(wlength-1, wlength);
    System.out.printf("first = %s, last = %s %n", first, last);
    if (first.compareTo(last) == 0) {
        System.out.printf("first == last%n");
        String inner= word.substring(1, wlength-1);
        System.out.printf("new Word = %s RECURSION%n", inner);
        isPalindrome(inner);
    } 
    System.out.println("END");
    return false;
    
}

Thanks!

6
  • prva is undefined. Jun 3, 2021 at 12:34
  • 2
    You're ignoring the return value of the inner call. All calls for lengths greater than 2 go at your last println and return false. Jun 3, 2021 at 12:35
  • Can you provide an example input word that causes the problem and post the corresponding console output, it's quite hard for anyone to diagnose what's going on otherwise. Suffice to say it definitely won't be 'ignoring' the return statement! Jun 3, 2021 at 12:35
  • 1
    isPalindrome(inner); -> return isPalindrome(inner);
    – VLAZ
    Jun 3, 2021 at 12:36
  • 1
    Also have you tried using a debugger to step through the code, it's a LOT more efficient than print statements. Jun 3, 2021 at 12:36

1 Answer 1

2
if (prva.compareTo(last) == 0) {
    System.out.printf("first == last%n");
    String inner= word.substring(1, wlength-1);
    System.out.printf("new Word = %s RECURSION%n", inner);
    isPalindrome(inner); // <--- here
} 

You haven't captured the return value on the last line shown above.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.