0

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());
    }
  }                                             
 }
abe
  • 131
  • 1
  • 1
  • 12
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Ousmane D. Apr 23 '17 at 00:19
  • don't compare strings like this --> `if (username == "admin")` change to this --> `if (username.equals("admin"))`. – Ousmane D. Apr 23 '17 at 00:24
  • You haven't break your while loop, so that why when the popup showing, it will increase i to 3 and break. – TuyenNTA Apr 23 '17 at 00:31
  • @TuyenNguyen I've tried doing that, but when I use break it just exits the loop and starts counting at i = 0 again. – abe Apr 23 '17 at 00:35
  • @abigail You have break your while loop, but you create a new login and show it up, so your counting must restart to 0. What's wrong? – TuyenNTA Apr 23 '17 at 00:39

0 Answers0