Hi,
I am working on a class that has to decide whether given number is palindrome or not. I need to consider two cases - when input is integer, but defined as a String and when input is defined as a number.
I am done with the first case - when the input is considered as a String.
Regarding the numerical input I have no idea where to start from. I want to try do it myself first before asking you, guys for advice.
So about the input, interpreted as a String.
This code is fine.Code:public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number: ");
int num = input.nextInt();
String str = String.valueOf(num);
StringBuilder sb1 = new StringBuilder(str);
StringBuilder sb2 = sb1.reverse();
if( sb2.toString().equals(str) )
System.out.print("\nPalindrome");
else
System.out.print("Not palindrome");
But when I change the if to
it always gives me that the number is a palindrome even if it is not.Code:if( sb2.equals(sb1))
System.out.print("\nPalindrome");
Why is that?

