Reputation:
I am trying to set some if-statements to ensure user inputs do not go out of ranges I have set if they do I want the if statement to return them to the beginning of the main code.
String animal = JOptionPane.showInputDialog("Enter In Animal Name"); // Asking for user to enter a animal
String fruit = JOptionPane.showInputDialog("Enter In A Fruit Name"); // Asking user to enter a fruit
int days = Integer.parseInt(JOptionPane.showInputDialog("Enter In How Many Days Between 1 And 10"));
// Above integer is asking the user to enter the amount of days.
// Below if statements are basically error checking to ensure the user stays between the
// range asked for when they are asked to enter in days between 1 and 10.
if (days <= 0) { // Ensures that negative numbers cannot be entered.
JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");
return;
}
if (days >= 10) { // Ensures nothing over 10 can be entered.
JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");
return;
}
If the if statement says that its an error it should go back to asking them to re enter the days
Upvotes: 1
Views: 69
Reputation: 159086
One way to allow code to restart a block of code, i.e. to go back to the beginning and try again, is to use the continue
and break
statements with a "forever" loop.
for (;;) { // loop forever
// some code here
if (failure condition 1) {
// handle failure here
continue; // go back and try again
}
if (failure condition 2) {
// handle failure here
continue; // go back and try again
}
// more code and failure condition checks here
break; // unconditional exit loop, since all is ok
}
If "some code here" is itself inside a loop, but need to go back to beginning and try again, you can use a label for that:
TRYAGAIN: for (;;) { // loop forever
// some code here
for (some looping here) {
// some code here
try {
if (failure condition) {
// handle failure here
continue TRYAGAIN; // go back and try again
}
} finally {
// code here will execute, even if 'continue' is used
}
}
// more code and failure condition checks here
break; // unconditional exit loop, since all is ok
}
Upvotes: 0
Reputation: 4667
Use a do-while
loop which will execute at least a single time, looping back every time if days
does not fulfill the conditions.
public static void main(String[] args)
{
do {
//Your other code
String animal = JOptionPane.showInputDialog("Enter In Animal Name"); // Asking for user to enter a animal
String fruit = JOptionPane.showInputDialog("Enter In A Fruit Name"); // Asking user to enter a fruit
int days = askForInput();
if (days <= 0 || days >= 10) { // Ensures that negative numbers cannot be entered.
JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");
}
} while (days <= 0 || days >= 10);
}
//Pass whatever parameters you might need
public static int askForInput() {
int days = Integer.parseInt(JOptionPane.showInputDialog("Enter In How Many Days Between 1 And 10"));
//Any other code you want
return days;
}
I also extracted it to a method and this might be unnecessary, but it will allow you add more functionality if you need it.
You can also move animal
or fruit
outside the do
if you do not want that question to be asked every time.
Upvotes: 1
Reputation: 755
Use a while loop
// Your user prompt for animal and fruit goes here
boolean exit = false;
while(!exit)
{
// Your user prompt for days code goes here
if (days <= 0 || days > 10) {
JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");
exit = false; // This is not necessary but nice for readability's sake
}
else {
exit = true;
}
}
Upvotes: 0
Reputation: 9058
String animal;
String fruit;
int days = 0;
animal = JOptionPane.showInputDialog("Enter In Animal Name");
fruit = JOptionPane.showInputDialog("Enter In A Fruit Name");
while(days <= 0 || days > 10) {
days = Integer.parseInt(JOptionPane.showInputDialog("Enter In How Many Days Between 1 And 10"));
if (days <= 0 || days > 10) {
JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");
}
}
Upvotes: 0