mardi 24 février 2015

how to have an else block execute only once in a for loop

I have a for loop which is used to compare user login details to launch the next screen of an application.


If the user-entered fields are successfully matched to data in an ArrayList returned from a database the program launches next screen -- if they are not matched an error message is outputted to the user using JOptionPane.


My problem is the error message is outputted for every iteration of the for loop but I want the message to only be displayed once.



//if name or password are NOT left blank proceed with the check otherwise output error message in the text area
if (!name.equals("") && !password.equals("")) {
for (int i = 0; i < passwords.size(); i++) {
if (name.equals(userNames.get(i)) && (password.equals(passwords.get(i)))) {
myHomeGUI.setVisible(true);
break;
} else {
JOptionPane.showMessageDialog(null,"Sorry, no user recognized with those credentials\nPlease try again");
}
}//end for loop
} else {
JOptionPane.showMessageDialog(null,"Sorry, no fields can be left blank");
}//end

Aucun commentaire:

Enregistrer un commentaire