I'm trying to use a break; statement to exit a for loop. The code will read in the username and password, verify it is a good login, then return to asking for the username and password, again.
final int NUM_USERS = 6; // Max Number of Users.
UserInfo[] users = new UserInfo[NUM_USERS]; // Array of Users.
int loginCounter = 0; // Counts bad login attempts.
int i = 0; // Loop index variable.
String userRole = ""; // Holds user's role text.
for (loginCounter = 1; loginCounter <= 3; ++loginCounter) {
// Get User's Credentials.
System.out.println("Enter Username: ");
String username = input.next().toLowerCase();
input.nextLine(); // Allows User to enter password.
System.out.println("Enter Password: ");
String password = input.nextLine();
// Convert password to MD5 hash.
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(); // Prints Good Login message.
break;
}
else {
sysLogin.badLogin(); // Prints Bad Login message.
}
}
}
}
What IS expected to happen, is once it hits a successfully login, it sets userRole to the current User's role, displayed the goodLogin message, then exits the loop.
Aucun commentaire:
Enregistrer un commentaire