Reputation: 93
I wrote this simple recursive method to check if a number is palindrome.
public boolean isPalindrome(int x) {
String str = Integer.toString(x);
if(str.length() <= 1){
return true;
}
else{
char first = str.charAt(0);
char last = str.charAt(str.length()-1);
if (first == last){
int short = Integer.parseInt(str.substring(1, str.length()-1));
return isPalindrome(short);
}
else{
return false;
}
}
}
However, I keep getting the compile error that this line
int short = Integer.parseInt(str.substring(1, str.length()-1));
is not a statement. Can anyone see what the problem is? Thanks!
Upvotes: 0
Views: 84
Reputation: 1545
Rename the variable name- short, it is a java keyword.
if (first == last){
return isPalindrome(Integer.parseInt(str.substring(1, str.length()-1)));
}
Upvotes: 1