Home > front end >  how to create a login Java Program with 3 password Attempt check
how to create a login Java Program with 3 password Attempt check

Time:11-12

am trying to create a java program which compares the stored string(meaning password stored) and another string to check password. and also trying to add a number of attempts like 3 in it. am almost done. but am still getting one small logical error. i tried so many changes and adding break statements to my knowledge but still the program isnt becoming perfect. if i change something then another logical error comes. everything works fine but this extra line occurs if i put right pass and right amount which i dont want."Account is blocked. Contact Bank Manager". how can i stop getting that. hope you guys will help me!

public class Test {
     int otherBankUserId=345;
     otherBankUserPass="suresh";
    int otherAcc;String otherPass=null;
    Scanner sc = new Scanner(System.in);
    
    
    public void checkOtherPass() {
        System.out.println("Enter the account");
        otherAcc = sc.nextInt();
        if(otherAcc==otherBankUserId)
        {
        while(passError<3) {
        System.out.println("Account number is verified. Enter the password");
        otherPass = sc.next();
                    
        if(otherPass.equals(otherBankUserPass)) {
        System.out.println("logged in successfully");
        System.out.println("Enter the amount:");
        int amountSum=sc.nextInt();
        System.out.println(amountSum "Rs withdraw successfully done. Remove the card!");    
        }
        else {
        System.out.println("wrong password!");
        passError=passError 1;
        System.out.println("Wrong Entries:" passError "\nMax Wrong pass entries:3");    
        }
                    
        }
        System.out.println("Account is blocked. Contact Bank manager");
        } 
                
        else {
        System.out.println("Wrong account number.Do the process again");
        }
        }
    
    public static void main(String[] args) {
        Test t = new Test();
        t.checkOtherPass();

    }

}

output: 
Enter the account 
345
Account number is verified. Enter the password
suresh
logged in successfully
Enter the amount:
333
333 withdraw successfully done. Remove the card!
Account is blocked. Contact Bank manager
..................................
everything works fine but this extra line occurs if i put right pass and right amount which i dont want."Account is blocked. Contact Bank Manager". how can i stop getting that

CodePudding user response:

You are printing the line you do not want after the loop is done regardless if the password was correct or not. It would help to format your code better with proper indentation to better see the problem.

There are several possible ways to solve this problem. Below, is an example using the value of passError and a break. Note that the value of this variable gets reset to 0 when the correct password is given.

        if(otherAcc==otherBankUserId)
        {
            while(passError < 3) 
            {
                System.out.println("Account number is verified. Enter the password");
                otherPass = sc.next();
                if(otherPass.equals(otherBankUserPass)) 
                {
                    passError = 0;
                    System.out.println("logged in successfully");
                    System.out.println("Enter the amount:");
                    int amountSum=sc.nextInt();
                    System.out.println(amountSum "Rs withdraw successfully done. Remove the card!");
                    break;
                } 
                else 
                {
                    System.out.println("wrong password!");
                    passError =1;
                    System.out.println("Wrong Entries:" passError "\nMax Wrong pass entries:3");    
                }
            }
            if (passError < 3) 
            {
                System.out.println("Thank you for your business");
            }
            else
            {
                System.out.println("Account is blocked. Contact Bank manager");
            }
        }
        else 
        {
            System.out.println("Wrong account number.Do the process again");
        }

CodePudding user response:

My hypothesis for this issue comes from the idea of scope:

...
if(otherAcc == otherBankUserId) 
{
    while(passError < 3) 
    {
        ...
    }
    System.out.println("Account is blocked. Contact Bank manager");
}
...

No matter what happens inside of this while-loop, the bottom line will be printed. The only exception would be a return-statement inside the while-loop which is considered bad practice.

Leaving the rest of your program as is, you could enter a case for that "Account is blocked ..." line:

if(attempts > 3) -> print that statement

EDIT: while you could use a break-statement once the solution is reached, it would be much better practice to create a condition around whether or not the solution is found for readability and debugging in the future.

while(not found && less than 3)
{
    keep asking;
}
  • Related