0

I am trying to limit the maximum login attempts to 3. However, my code below uses all the attempts before the user can get a chance to press the login button again. How would I fix this?

private void executeLogin() {

    String userNameStr = userNameTF.getText();
    String passWordStr = passWordTF.getText();

    int totalAttempts = 3;

    while (totalAttempts != 0) {

        if (userNameStr == "temp" && passWordStr == "pass") {


            System.out.println("Login Correct!");
            return;

        } else {


            System.out.println("Incorrect Login");

            totalAttempts--;
            System.out.println(totalAttempts);

        }

    }

    if (totalAttempts == 0) {

        System.out.println("Maximum number of attempts exceeded");
    }
}
Milan
  • 763
  • 3
  • 10
  • 18
  • I am guessing `userNameTF` and `passWordTF` are text fields - so you have some sort of UI... Well actually you do not need loop here. This function should be called only when user presses login button. and totalAttempts should be global variable... – PKey Aug 17 '16 at 16:31

7 Answers7

2

So there are 3 main problems with your code:

  1. You use while loop - though you shouldn't loop at all, function should be called every time login button is pressed.

  2. Number of attempts cannot be local variable - you should keep its value for future use (so global variable then)

  3. You are comparing string the wrong way (not == but equals)

    public class MyForm extends ... {
    
     int totalAttempts = 3;
     private void login() {
     String userNameStr = userNameTF.getText();
     String passWordStr = passWordTF.getText();
    
     if (totalAttempts != 0) 
         if ("temp".equals(userNameStr) && "pass".equals(passWordStr)) 
                    System.out.println("Correct");
    
                else {
                     System.out.println("Incorrect");
                     totalAttempts--;    
                     } 
     else 
         System.out.println("Maximum number of attempts exceeded");
    
    
       }
     }
    
PKey
  • 3,715
  • 1
  • 14
  • 39
2

Whenever, the executeLogin() will be invoked, the previous value of totalAttempts will be erased and it will be again initialized to 3. To control this, you can make the totalAttempts as global

int totalAttempts= 3; 
private void executeLogin() {

    String userNameStr = userNameTF.getText();
    String passWordStr = passWordTF.getText();

    if (totalAttempts != 0) {
        if (userNameStr == "temp" && passWordStr == "pass") {
            System.out.println("Correct");
        else {
            System.out.println("Incorrect");
            totalAttempts--;

    } else {
        System.out.println("Maximum number of attempts exceeded");
    }
}

Or if you are declaring it inside the class make it static.

Prakash
  • 13
  • 6
Neha Dadhich
  • 1,043
  • 8
  • 5
  • Thank you Neha for your code. While others did also answer the question and did provide a solution, your answer was fairly thought through with some pointers on what I should and should not do. For this reason, I shall choose yours as the best answer. Thank You, everyone else! – Milan Aug 17 '16 at 17:34
0

Your problem is that the while loop doesn't wait for the user to press the button again. Assuming that executeLogin() is called every time the button is pressed, you need to keep a global attempts variable and decrement it every time the method is called rather than multiple times within the same call.

int totalAttempts = 3;

private void executeLogin() {

    String userNameStr = userNameTF.getText();
    String passWordStr = passWordTF.getText();

    if (totalAttempts != 0) {
        if (userNameStr == "temp" && passWordStr == "pass") {
            System.out.println("Correct");
        else {
            System.out.println("Incorrect");
            totalAttempts--;

    } else {
        System.out.println("Maximum number of attempts exceeded");
    }
}
elaid
  • 351
  • 1
  • 10
0

Your code just performs the same login totalAttempts times. You don't cede control. In event-driven programming, you do not wait for the user to do something, you set it up and do things in response to a user doing something. In other words, you never write a code that actively waits for a user to input the password again.

The main thing is, there is no such thing as "limiting the number of logins to 3". You can limit the number of consecutive incorrect logins to 3, you can limit the number of consecutive incorrect user logins to 3, you can limit the number of incorrect logins per period to 3 - all of those require some data structure to hold the information about the failed logins.

Piotr Wilkin
  • 3,446
  • 10
  • 18
0

The problem in your code is that you don't request the user to enter his username and password again. My fix would be:

Design the function so that it requests the credentials in the loop

private void executeLogin() {

    String userNameStr;
    String passWordStr;

    int totalAttempts = 3;

    while (totalAttempts != 0) {

        userNameStr = userNameTF.getText();
        passWordStr = passWordTF.getText();

        if (userNameStr == "temp" && passWordStr == "pass") {

            System.out.println("Login Correct!");
            return;

        } else {


            System.out.println("Incorrect Login");

            totalAttempts--;
            System.out.println(totalAttempts);

        }

    }

    if (totalAttempts == 0) {

        System.out.println("Maximum number of attempts exceeded");
    }
}
Kostas Pelelis
  • 1,322
  • 9
  • 11
  • This will have the same problem - cause user will never have a chance to change his input... – PKey Aug 17 '16 at 16:34
  • Is userNameTF.getText() a function that waits for user input? If not read the username and password from command line like ```Java BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter your username: "); String username = reader.readLine(); System.out.print("\nEnter your password: "); String password = reader.readLine(); ``` – Kostas Pelelis Aug 17 '16 at 16:40
0
package javaconcepts;

import java.util.Scanner;

public class JavaLoginPrg {

    private void login() {
        String username, password;
        int totalAttempts = 3;
        while (totalAttempts != 0) {

            if (username == "temp" && password == "pass") {
                System.out.println("Login succeeded!");
                return;
            } else {
                System.out.println("Login failed!");
                totalAttempts--;
                System.out.println(totalAttempts);
            }
        }
    }

    public static void main(String[] args) {
        String temp, pass;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter username");
        username = sc.toString();
        System.out.println("Enter password");
        password = sc.toString();
    }
}
Ashok kumar Ganesan
  • 1,098
  • 5
  • 20
  • 48
  • Hi @HerramaniMadhwani - plese read about [the difference between equals and ==](https://stackoverflow.com/questions/7520432/what-is-the-difference-between-and-equals-in-java) - the code can never work !! – Mr R Apr 07 '22 at 11:24
0

Tried with user prompted inputs and it actually works. Here:

import java .util.Scanner;

public class AuthenticateUser {

public static void main(String[] args) {

    int attempts=0;
    String password="password";
    Scanner obj = new Scanner(System.in);

    while (true)
    {

        if(attempts!=5)
        {
            System.out.println("Enter your password ");
            String inputPassword = obj.nextLine();

            if (inputPassword.equals(password)) {
                System.out.println("You have successful logged in!");
                break;
            } else {
                System.out.println("Something went wrong, verify your password and try again!");
                attempts++;
            }
        } else {
            System.out.println("You incorrectly logged in 5 times, you will have to wait for 30 minutes");
            break;
        }

    }
}

}
Sha
  • 921
  • 17
  • 46