I am trying to use an instruction break;to exit a loop for.
final int NUM_USERS = 6;
UserInfo[] users = new UserInfo[NUM_USERS];
int loginCounter = 0;
int i = 0;
String userRole = "";
for (loginCounter = 1; loginCounter <= 3; ++loginCounter) {
System.out.println("Enter Username: ");
String username = input.next().toLowerCase();
input.nextLine();
System.out.println("Enter Password: ");
String password = input.nextLine();
String hash = sysLogin.convertToMd5(password);
for (i = 0; i < users.length; ++i) {
if (username.equals(users[i].getUsername())) {
if (hash.equals(users[i].getHash())) {
userRole = users[i].getRole();
sysLogin.goodLogin();
break;
}
} else {
sysLogin.badLogin();
}
}
}
What's happening:
The code will be read in usernameand passwordcheck that it is a good login, then go back to the username and password.
What is expected to happen:
After a successful login, it sets the userRolecurrent user role, displays a message goodLogin, and then exits the loop.
source
share