Answer to Question #51197 in Java | JSP | JSF for Michael

Question #51197
how to ask user to reenter ID number if the number entered is invalid?
inputString = JOptionPane.showInputDialog(null, "Enter the student ID number");
IDNumber = Integer.parseInt(inputString);

for(x = 0; x < studentsIDNumberList.length && !validIDNumber; ++x)
{
if(IDNumber == studentsIDNumberList[x])
{
validIDNumber = true;
firstName = studentsFirstNameList[x];
GPA = studentsGPAArray[x];
}
}

if(validIDNumber)
{
JOptionPane.showMessageDialog(null, "The ID number you entered is " + IDNumber + ". This number belong to "
+ firstName + " and this student GPA is " + GPA);
}

else
{
JOptionPane.showMessageDialog(null, "Invalid ID number!");
}
1
Expert's answer
2015-03-10T04:19:44-0400
Solution.
1. You need to add an infinite loop (while (true))
2. Out of the loop (break;)

while(true)
{

inputString =JOptionPane.showInputDialog(null, "Enter the student ID number");
IDNumber =Integer.parseInt(inputString);

for(x = 0; x < studentsIDNumberList.length && !validIDNumber; ++x)
{
if(IDNumber == studentsIDNumberList[x])
{
validIDNumber = true;
firstName =studentsFirstNameList[x];
GPA =studentsGPAArray[x];
}
}

if(validIDNumber)
{
JOptionPane.showMessageDialog(null, "The ID number you entered is " +IDNumber + ". This number belongto "
+ firstName + " and this student GPA is " + GPA);

break;
}
else
{
JOptionPane.showMessageDialog(null, "Invalid ID number!");
}

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS