0

I'm making a java program that checks the user's input to see if it's a palindrome. My code is below, but at the:

if(isPalindrome() = true)
     System.out.println("You typed a palindrome!");

part I get the error saying "the left side of the assignment must be a variable." isn't it a variable? and what can I do to fix it? Any advice is appreciated!

public class PalindromeChecker
{
public static void main(String [] args)
{
    String answer;
    while(answer.equalsIgnoreCase("y"))
    {
        System.out.println("Please enter a String of characters.  I will check to see if");
        System.out.println("what you typed is a palindrome.");
        Scanner keys = new Scanner(System.in);
        String string = keys.nextLine();
        if(isPalindrome() = true)
            System.out.println("You typed a palindrome!");
        else
            System.out.println("That is not a palindrome.");
        System.out.print("Check another string? Y/N: ");
        answer = keys.next();
    }
}

public static boolean isPalindome(String string)
{
    if(string.length() <= 0)
        System.out.println("Not enough characters to check.");
    string = string.toUpperCase();
    return isPalindrome(string,0,string.length()-1);
}

private static boolean isPalindrome(String string, int last, int first)
{
    if(last <= first)
        return true;
    if(string.charAt(first) < 'A' || (string.charAt(first) > 'Z'))
        return isPalindrome(string,first + 1, last);
    if(string.charAt(last) < 'A' || (string.charAt(last) > 'Z'))
        return isPalindrome(string,first, last - 1);
    if(string.charAt(first) != string.charAt(last))
        return false;
    return isPalindrome(string,first + 1, last - 1);
}
}

3 Answers 3

3

Use double equals == for comparisons. A single equal sign = is the assignment operator.

if (isPalindrome() == true)

Or better yet, for boolean comparisons don't use == at all. It reads more like English if you just write:

if (isPalindrome())
Sign up to request clarification or add additional context in comments.

4 Comments

There's no isPalindrome(), though.
@usernametbd Yeah, there are various other problems the OP will need to deal with after this one. Not just compile-time errors, either.
tried this and I'm getting "The method isPalindrome(String, int, int) in the type PalindromeChecker is not applicable for the arguments (String)"
@Derek Think about what that error message is saying and see if you can figure out the problem. You're going to have to learn to interpret these compiler error messages. If you try and still don't understand, post a separate question.
1

Your method call should be: isPalindrome expecting String parameter:

if(isPalindome(string ))

And you don't need to do equality check because return type is anyway boolean.

2 Comments

tried this and I'm getting "The method isPalindrome(String, int, int) in the type PalindromeChecker is not applicable for the arguments (String)"
It should be isPalindome, not isPalindrome, observe r missing. See my edit.
0

Use

if(isPalindome(string)==true)

Instead.

Two changes:

1) You need to pass string to isPalindome.

2) You need to use two equal signs, not just one, for the sake of comparison.

Also, I believe you may have meant to write "isPalindrome" instead of "isPalindome"

4 Comments

i tried this and I'm getting "The method isPalindrome(String, int, int) in the type PalindromeChecker is not applicable for the arguments (String)"
Again, you put isPalindome instead of isPalindrome for one of your method names- fix this and the problem should go away.
facepalm wow I feel smart. haha thanks man I appreciate it :P
Don't worry about it, happens to the best of us. No problem!

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.