Here is my question. It's asking to return true
if int x
is a palindrome otherwise return false
:
public class Palindrome {
public boolean isPalindrome(int x) {
StringBuilder number = new StringBuilder(Integer.toString(x));
return (number.reverse() == number) ? true : false;
}
public static void main(String[] args) {
Palindrome object = new Palindrome();
boolean state = object.isPalindrome(45678);
System.out.println(state);
}
}
I think my logic makes perfect sense here. If the reverse of the number is equal to the original number , return true (121 = 121)
. How is 87654 = 45678?
Can you explain why my method doesn't work?