So I'm making a palindrome checker in java, and i've seemed to hit a roadblock this is my code so far:
public class StringUtil
{
public static void main(String[] args)
{
System.out.println("Welcome to String Util.");
Scanner word = new Scanner(System.in);
String X = word.nextLine();
String R = palindrome(X);
System.out.println();
System.out.println("Original Word: " + X);
System.out.println("Palindrome: " + R);
}
public static boolean palindrome(String word)
{
int t = word.length(); //length of the word as a number
int r = 0;
if(word.charAt(t) == word.charAt(r))
{
return true;
}
else
return false;
}
so far I only want it to check if the first letter is the same as the last, but when i compile it i get an incompatible types error on "String R = palindrome(X);" How would i get it to print true or false on the output statement below it?
Your palindrome
method returns a boolean
, but you're attempting to assign it to a String
. Change the definition of the R
variable to boolean
.