Reputation: 107
After making an invalid entry, the invalid entry loop continues to appear in this program. I am seeking to keep asking the user for input until he/she enters valid data. Then I'd like to use that input to continue on with the program. I'm positive there's a type of loop associated with this. Thanks for your help.
identInput = JOptionPane.showInputDialog(null, "Please enter a student ID: ");
intID = Integer.parseInt(identInput);
savedIntID = Integer.parseInt(identInput);
for(x = 0; x < studentIDs.length; ++x)
{
if(identInput.equals(studentIDs[x]))
{
JOptionPane.showMessageDialog(null, "The first name associated with \nStudent ID "
+ intID + " is: " + firstNames[x] + "\n" + firstNames[x] + "'s current GPA is: "
+ gPAs[x]);
inputTruth = true;
break;
}
}
//The following will show up and continue to if the data is incorrect. Am not
//sure how to reuse if good data are entered.
while(inputTruth == false)
{
JOptionPane.showInputDialog(null, "The Student ID you entered "
+ savedIntID + "\nis not valid. \nPlease enter another Student ID: ");
}
Upvotes: 0
Views: 795
Reputation: 762
is this what you want ?
inputTruth = false;
while(inputTruth == false)
{
for(x = 0; x < studentIDs.length; ++x)
{
if(identInput.equals(studentIDs[x]))
{
JOptionPane.showMessageDialog(null, "The first name associated with \nStudent ID "
+ intID + " is: " + firstNames[x] + "\n" + firstNames[x] + "'s current GPA is: "
+ gPAs[x]);
inputTruth = true;
break;
}
}
if (inputTruth == false) {
identInput = JOptionPane.showInputDialog(null, "The Student ID you entered "
+ savedIntID + "\nis not valid. \nPlease enter another Student ID: ");
}
}
Upvotes: 1
Reputation: 2923
inputTruth = false;
while(inputTruth == false)
{
identInput = JOptionPane.showInputDialog(null, "Please enter a student ID: ");
intID = Integer.parseInt(identInput);
savedIntID = Integer.parseInt(identInput);
for(x = 0; x < studentIDs.length; ++x)
{
if(identInput.equals(studentIDs[x]))
{
JOptionPane.showMessageDialog(null, "The first name associated with \nStudent ID "
+ intID + " is: " + firstNames[x] + "\n" + firstNames[x] + "'s current GPA is: "
+ gPAs[x]);
inputTruth = true;
break;
}
}
//The following will show up and continue to if the data is incorrect. Am not
//sure how to reuse if good data are entered.
if(inputTruth == false)
{
JOptionPane.showInputDialog(null, "The Student ID you entered "
+ savedIntID + "\nis not valid. \nPlease enter another Student ID: ");
}
}
Upvotes: 1
Reputation: 1976
You just Need a while loop
while(!isInputValid){
//Take your input
if(check == input){
isInputValid = true;
}else{
//Please enter valid input try again.
}
}
Upvotes: 1