It must throw NumberFormatException if you try to parse it to integer.
Check before parsing. or handle Exception properly.
Exception Handling*
try{
int i = Integer.parseInt(input);
}catch(NumberFormatException ex){ // handle your exception
...
}
or
- Integer pattern matching -
String input=...;
String pattern ="-?\\d+";
if(input.matches("-?\\d+")){ // any positive or negetive integer or not!
...
}
solved I’m a Java beginner- Convert String value to Integer using JTextField [closed]