Java palindrome exercise


#1

Hello everyone,

the following code is a palindrome exercise I am working out in eclipse. the exercise is to return whether the given user input is a palindrome or not. Can anyone help with this?

public class Palindrome {
public static void main(String args) {
try {
System.out.println(isPalindrome());
} catch (Exception e) {
System.out.println(“Can’t input empty string!”);
e.printStackTrace();
}
}
public static boolean isPalindrome() throws Exception{
Scanner input = new Scanner(System.in);
System.out.println(“Type in a phrase and press Enter to see if the word is a Palindrome…”);
String phrase = input.nextLine();
int length = phrase.length();
int c = 0;

            while (c <= length/2) {
                System.out.println(length/2);
                System.out.println(length - 1 - c);
                if (input.charAt(c) != input.charAt(length - 1 - c)) {
                    return false;
                }
                c++;
            }
            return true;
        }
}

With this code, eclipse is giving me an error towards the bottom in my ‘if’ statement. The error is - the method charAt(int) is undefined for the type Scanner.

Thank you!


#2

I found this, which may give a hint…