Eogcloud
Eogcloud

Reputation: 1365

Double string comparison using recursion

I'm having some trouble with a question from my programming II class and have hit a brick wall, was wondering if someone could help?

The question asks for a user to input a string, the program to reverse the input string and then to compare the reverse to the original, this must be done recursively.

So far I have:

public class question1 
{
public static void main(String args[])  
{

String input = JOptionPane.showInputDialog(null, "Please enter a sentence to determine if it is a palindrome.");
String backwardsinput = Reverse(input);
System.out.println(backwardsinput);
boolean Palindrome = PalindromeCheck(backwardsinput, input);

    if (Palindrome == true)

        {
        JOptionPane.showMessageDialog(null,"That is a palindrome!");
        }

    if (Palindrome == false)

        {
        JOptionPane.showMessageDialog(null,"That is not a palindrome"); 
        }

}

public static String Reverse (String input)
{
    if (input.length() <= 1)
    return input;

    else
    {
        char x = input.charAt(input.length()-1);               
        return x+Reverse(input.substring(0,input.length()-1));
    }

}



public static boolean PalindromeCheck (String backwardsinput, String input)
{

     if(input.length() == 0 || input.length() == 1)
            return true;

        if(backwardsinput.charAt(0) == input.charAt(input.length()-1))
            return PalindromeCheck(backwardsinput.substring(1, backwardsinput.length()-1), input.substring(1, input.length()-1));

        else
        return false;

       }    
}

My problem is, it tells me everything is a palindrome, I've looked at it over and over and can't figure out why!

Upvotes: 5

Views: 756

Answers (3)

aioobe
aioobe

Reputation: 421110

You're doing the work twice (sort of).

if(backwardsinput.charAt(0) == input.charAt(input.length()-1))
                                            ^^^^^^^^^^^^^^^^

should be

if (backwardsinput.charAt(0) == input.charAt(0))
                                             ^

You almost got it :-)


Also, another way of expressing

if (cond)
    return something;
else
    return false;

is

return cond && something;

thus your last lines could be written as

return backwardsinput.charAt(0) == input.charAt(0) &&
       palindromeCheck(backwardsinput.substring(1, backwardsinput.length() - 1),
                                input.substring(1, input.length() - 1));

Related question / answer:

Upvotes: 6

Oliver
Oliver

Reputation: 11617

if(backwardsinput.charAt(0) == input.charAt(input.length()-1))

This doesn't work. You want backwardsinput.charAt(0) == input.charAt(0). because you reversed it.

When I debug something like this, I tend to pull apart all my compound statements, so

if(backwardsinput.charAt(0) == input.charAt(input.length()-1))

would become

char bc = backwardinput.charAt(0);
char ic = input.charAt(input.length()-1);
System.println(bc);
System.println(ic);
if (bc == ic) {

We all make dumb mistakes when we try to put too much in one expression, so these are good places to start looking for logic errors.

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533700

You are reversing the string and then checking the string matchs this reversed. i.e. it tests that the String was reversed correctly.

When you can do is not reverse the string and then only check a String matches itself (ratehr than its reverse) backwards.

Upvotes: 2

Related Questions