do{
try{
input = (String) JOptionPane.showInputDialog(null,"Food quantity","Dope Alley",JOptionPane.QUESTION_MESSAGE);
quantity = Integer.parseInt(input);
if(quantity <= 0 || quantity > 100){
JOptionPane.showMessageDialog(null,"Invalid input . Number only .\n\nMininum order = 1\nMaximum order = 100","Dope Alley",JOptionPane.ERROR_MESSAGE);
}
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null,"Invalid input . Number only .\n\nMininum order = 1\nMaximum order = 100","Dope Alley",JOptionPane.ERROR_MESSAGE);
}
}while(quantity <= 0 || quantity > 100);
catch(NumberFormatException e) how to clarify variable e in my code? in my code the program stated that variable e is not used . so how can i used it
You could use a regular expression. \\d+ will match consecutive digits. Also, I'd suggest a do-while loop. Something like
int quantity = -1;
do {
input = (String) JOptionPane.showInputDialog(null, "Food quantity",
"Dope Alley", JOptionPane.QUESTION_MESSAGE);
if (input.matches("\\d+")) {
quantity = Integer.parseInt(input);
}
} while(quantity < 1 || quantity > 100);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With