I Have an JOptionPane that I want to convert to integer, but only if it actually is an integer the user inserts. How do I do this? if-statement?
2 Answers
You can just parse and catch the exception:
Integer value;
try{
value = Integer.valueOf(input);
} catch(NumberFormatException ignored) {
value = 0;
}
Alternatively you can use a regular expression:
Integer value;
if (input.matches("\\d+")) {
value = Integer.valueOf(input);
} else {
value = 0;
}