-1

How can I stop it from looping? The error msg box saying about invalid username and password will keep pop out based on the numbers of user I have in my textfile, I didn't know how to solve it.

for (int i = 0; i < userList.size(); i++) {
            if (userList.get(i).getUserID().equals(txtUserID.getText()) && userList.get(i).getPassword().equals(ptxtPassword.getText())) {
                if (userList.get(i).getUserType().equals("Administrator")) {
                    System.out.println("Welcome Admin " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");
                    mainMenuForm.lblUser.setText("Welcome Admin " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

                    mainMenuForm.setVisible(true);

                    this.setVisible(false);

                } else if (userList.get(i).getUserType().equals("Sales Manager")) {
                    System.out.println("Welcome SM" + userList.get(i).getName());
                    mainMenuForm.lblUser.setText("Welcome SM " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

                    mainMenuForm.setVisible(true);
                    mainMenuForm.lblRegistration.hide();

                    this.setVisible(false);

                } else if (userList.get(i).getUserType().equals("Purchase Manager")) {
                    System.out.println("Welcome PM" + userList.get(i).getName());
                    mainMenuForm.lblUser.setText("Welcome PM " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

                    mainMenuForm.setVisible(true);
                    mainMenuForm.lblDailySales.hide();

                    this.setVisible(false);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Invalid User ID or Password!", "Login Error", JOptionPane.OK_OPTION);
            }
        }
eis
  • 51,991
  • 13
  • 150
  • 199
J.Doe
  • 3
  • 1

4 Answers4

1

To break out of loop in java, break is the command used.

What you want is something like this, in pseudocode:

credentialsCorrect = false;
loop(usersList) {
  if (correct credentials) {
    credentialsCorrect = true;
    break;
  }
}
if (credentialsCorrect) {
  showCorrectCredentialsResponse();
} else {
  showWrongCredentialsResponse();
}

Or even better, a map with user id as the key, so you don't need to do manual looping:

userData = usersMap.get(userId);
credentialsCorrect = userData != null && matches(userData, password)
if (credentialsCorrect) {
  showCorrectCredentialsResponse();
} else {
  showWrongCredentialsResponse();
}
eis
  • 51,991
  • 13
  • 150
  • 199
1

If I understood your question correctly, you can use the break Keyword to break out of a loop. If you want to tidy up your code, you can replace the if statements with a switch statement, to improve readability.

0

get rid of

for (int i = 0; i < userList.size(); i++) {

to stop it from looping for every user

Doug
  • 5,661
  • 2
  • 26
  • 27
0

You can use a break statement, but I would advice against it. Check this answer for more information.

You have the message display inside the for loop. You can move it out and control wether you have found the user/password or not with a boolean variable. The message should be visible only once if there is no such user/password combination:

boolean found = false;
for (int i = 0; i < userList.size() && !found; i++) {
    if (userList.get(i).getUserID().equals(txtUserID.getText()) && userList.get(i).getPassword().equals(ptxtPassword.getText())) {
        found = true;
        if (userList.get(i).getUserType().equals("Administrator")) {
            System.out.println("Welcome Admin " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");
            mainMenuForm.lblUser.setText("Welcome Admin " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

            mainMenuForm.setVisible(true);

            this.setVisible(false);

        } else if (userList.get(i).getUserType().equals("Sales Manager")) {
            System.out.println("Welcome SM" + userList.get(i).getName());
            mainMenuForm.lblUser.setText("Welcome SM " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

            mainMenuForm.setVisible(true);
            mainMenuForm.lblRegistration.hide();

            this.setVisible(false);

        } else if (userList.get(i).getUserType().equals("Purchase Manager")) {
            System.out.println("Welcome PM" + userList.get(i).getName());
            mainMenuForm.lblUser.setText("Welcome PM " + userList.get(i).getName() + " (" + userList.get(i).getUserID() + ")");

            mainMenuForm.setVisible(true);
            mainMenuForm.lblDailySales.hide();

            this.setVisible(false);
        }
    }
}

// if we didn't find the user/password combination, display the error message
if(!found) {
    JOptionPane.showMessageDialog(null, "Invalid User ID or Password!", "Login Error", JOptionPane.OK_OPTION);
}
Bernat
  • 492
  • 5
  • 13