I am trying to complete a task and I am unsure what route to take. I have tried while
, if
, and a combination of statements and cannot get the input validation I need.
- I am trying to validate user input and ensure their input is a number between 0 and 10 (excluding 0 and 10).
- Also I need to make sure that what they do enter is a number and not some symbol or letter.
- Finally I need a counter that will allow them 3 chances to input the correct information.
The code below is my method I am trying to setup to accomplish this.
private static int getNumericInput(String quantity) {
int count = 0;
String input;
input = JOptionPane.showInputDialog(quantity);
int range = Integer.parseInt(input);
while ((range > 9 || range < 1) && (count < 2)) {
JOptionPane.showMessageDialog(null,
"Sorry that input is not valid, please choose a quantity from 1-9");
input = JOptionPane.showInputDialog(quantity);
count++;
}
if (count == 2) {
JOptionPane.showMessageDialog(null,
"Sorry you failed to input a valid response, terminating.");
System.exit(0);
}
return range;
}