I encounter some problems parsing a string into integer, I don't know what the problem is.
I am passing a string with an integer value to my function to check if it would be parseable.
This is my function.
private boolean isNumeric (String value){
try {
System.out.println("VALUE = " +value);
int x = Integer.parseInt(value);
return true;
} catch (NumberFormatException e) {
System.out.println("NumberFormatException: " + e.getMessage());
return false;
}
}
I checked it by printing into the console. here is the result.
I/System.out: VALUE = 77
NumberFormatException: Invalid int: "77"
Can you guys help me figure out what is wrong here. Cause I cannot convert this string into an integer.
You could try this as this will trim out all whitespaces from your input:
private boolean isNumeric (String value){
try {
System.out.println("VALUE = " +value);
int x = Integer.parseInt(value.trim());
return true;
} catch (NumberFormatException e) {
System.out.println("NumberFormatException: " + e.getMessage());
return false;
}
}
parseInt()
should return a NumberFormatException if the string is not parseable, but "77" should be. You should therefore try to trim it before you parse it to an int, maybe that would help.