0

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: ");
}

3 Answers 3

1
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: ");
}
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, although same as the comment above, the program continues to ask for valid inputs even after correct data is entered. Any ideas on how to reuse data?
If you it does not work with correct data, the problem is in if(identInput.equals(studentIDs[x])). Try to log what is there with System.out.println(...) and also log the stundentIDs[x].
1

You just Need a while loop

while(!isInputValid){
 //Take your input
 if(check == input){
   isInputValid = true;
 }else{
  //Please enter valid input try again.
 }
}

Comments

1

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: ");
  }
}

3 Comments

sort of, this works, but after the program recognizes the incorrect data, it just continues to ask... regardless if correct data is entered thereafter. I appreciate your help though!
my mistake, would you please try this one? feel free to tell me if it doesn't work
No worries, your help is appreciated. What needed to happen is to start the while loop before the user make his/her input. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.