how to limit 3 login attempts?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zsazsa
    New Member
    • May 2015
    • 2

    how to limit 3 login attempts?

    here are my codes
    Code:
    int counter = 0;
            int i = 0;
            // for (int i = 0; i <= 3; i++) {
            while (i < 3) {
                String au = ausernametxt.getText();
                String ap = apasswordtxt.getText();
    
                String pass = "111";
                String user = "zsa";
    
                if (user.equals(ausernametxt.getText()) && (pass.equals(apasswordtxt.getText()))) {
                    JOptionPane.showMessageDialog(null, "Successfully log in!");
                    new menu().setVisible(true);
    
                }
    
                if (user != (ausernametxt.getText()) && (pass.equals(apasswordtxt.getText()))) {
                    JOptionPane.showMessageDialog(null, "Invalid User No", "LOG IN", JOptionPane.ERROR_MESSAGE);
                    counter += 1;
                    System.out.println(counter);
    
                }
    
                if (user.equals(ausernametxt.getText()) && (pass != (apasswordtxt.getText()))) {
                    JOptionPane.showMessageDialog(null, "Invalid Password", "LOG IN", JOptionPane.ERROR_MESSAGE);
                    counter++;
                    System.out.println(counter);
    
                } else {
                    
               
                    JOptionPane.showMessageDialog(null, "You Inputed 3 wrong password/Username", "LOG IN", JOptionPane.ERROR_MESSAGE);
    
                    System.exit(0);
                }
    
            }
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Delete the else in line 29. So if the counter reaches 3, your loop ends and lines 32 to 34 will be executed.
    Of course you don't want to execute these two lines if you successfully logged in, so insert a "return" at line 14.
    Last not least change the "if" in line 24 to an "else if", becuase you don't want to execute the following lines if you already executed 18 to 20.

    Comment

    • zsazsa
      New Member
      • May 2015
      • 2

      #3
      Thank you for the answer :)

      Comment

      Working...