How check number on symmetrics?
public static int Symmetric(int a) {
if(new StringBuilder(Integer.toString(a)) ==
new StringBuilder(Integer.toString(a)).reverse())
return a;
else
return 0;
}
I try do it smth like this but always return 0.
You can't use ==
to compare String
s (or StringBuilder
s), you need to use equals()
.
Also, you need to turn the StringBuilder
s back to String
s before comparing:
EDIT:
Also, there is really no need for the first StringBuilder
:
public static int symmetric(int a) {
if (Integer.toString(a).equals(new StringBuilder(Integer.toString(a)).reverse().toString()))
return a;
else
return 0;
}