I’m prompting the user to input an integer and if they don’t enter a proper integer (as a reference to an option) then I would like the prompt to show up again until they do.
So far this is the code I have:
int logIn = 0;
do {
logIn = Integer.parseInt(JOptionPane.showInputDialog(null,
"Please:"
+ "\n(Enter number value of option you would like to choose.)\n"
+ "\n1. Log In \n2. Register"));
} while (1 > logIn || logIn < 2);
int custIndex;
if (logIn == 1) {
custIndex = recommend.getCustomerIndex();
} else {
customers.printCustomers();
custIndex = customers.readCustomers().size();
}
int options = 0;
do {
options = Integer.parseInt(JOptionPane.showInputDialog(null,
"Would you like to:"
+ "\n(Enter number value of option you would like to choose.)\n"
+ "\n1. See your recommendations. \n2. See top rated books."
+ "\n3. See random books of the day. \n4. Exit."));
} while (1 > options || options < 4);
The only problem is that my application won’t get past the log in correctly. If the user enters 1, it shows them the prompt again; and if the user enters any number higher than 2, it takes them to the second option no matter what.
Any help would be appreciated.
Your
whilecondition readswhich means the input must be less than 1 or less than 2 for the loop to continue. What you want is
or perhaps more legibly