I'm having a problem with my java login authentication code. I want it to only allow 3 attempts. After each attempt, it has to reopen the login GUI. It does so, but then the JOptionPane with the message "You've used x out of 3 tries" keeps popping up and counting attempts even though I haven't been allowed to enter anything into the login.
The code searches through a csv file for the username and password information and verifies them. Using correct information, logging in is not a problem. My problem is the loop specifically, not the actual verification of my password. I know that the password/username authentication does work. It's the logic of the loops that is giving me errors.
Any help is greatly appreciated, thank you!
String username = usernameText.getText();
char[] passwrd = passwordField.getPassword();
String password = new String(passwrd);
boolean blnFound = false;
statement.executeQuery("SELECT username, password FROM users WHERE username='" + username + "' " +
"AND password='" + password + "';");
ResultSet rs = statement.getResultSet();
blnFound = rs.first();
System.out.println("blnFound. Closing log in window.\n");
// close login window upon pressing enter
dispose();
int i = 0;
while (i < 3) {
if (blnFound) {
if (username == "admin") {
//pass along value of username
//add admin class
} else {
//regular user
//pass along value of username
newTicket nw = new newTicket();
nw.username(username);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new user().setVisible(true);
}
});
}
} else { //incorrect password code
i++;
String message = "Username or password incorrect."
+ "\n\n" + "You have used " + i + " out of 3 tries.";
System.out.println("Show Dialog.");
JOptionPane.showMessageDialog(null, message);
System.out.println("Restart login.");
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new logIn().setVisible(true);
}
});
}
}
if (i == 3) {
String end = "You have attempted too many times.";
JOptionPane.showMessageDialog(null,end);
System.exit(0);
}
statement.close();
connect.close();
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
}
}