So I'm learning java...it's pretty cool so far, very similar to the c++.
I've written a short input loop. The user is asked for a value and the loop will give an error if the input is out of range or not a number then ask for the value again.
If the user hits cancel, the loop is finished.
How does this look? Is it bullet proof? Does it follow best practices? How are the variable names?
Thanks in advance for any suggestions.
Code:
int boardSize = -1;
while(boardSize == -1) {
try {
String intStr = (JOptionPane.showInputDialog(null,
"Enter an integer of value 1 through " + MAXIMUM_NUMBER_OF_ROWS,
"How many rows?",
JOptionPane.QUESTION_MESSAGE));
//check if the user has canceled the input box
if(intStr == null) {
break;
}
boardSize = Integer.parseInt(intStr);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"Error: you must enter an integer of value 1 through " + MAXIMUM_NUMBER_OF_ROWS,
"Invalid Input",
JOptionPane.ERROR_MESSAGE);
boardSize = -1;
continue;
}
if((boardSize > MAXIMUM_NUMBER_OF_ROWS) || (boardSize < 1)) {
JOptionPane.showMessageDialog(null,
"Error: you must enter an integer of value 1 through " + MAXIMUM_NUMBER_OF_ROWS,
"Invalid Input",
JOptionPane.ERROR_MESSAGE);
boardSize = -1;
}
}