user5770672
user5770672

Reputation:

Creating java code to prompt user for password and allow only 3 attempts then quit the programme

I have been experimenting with trying to get the code below running WITHOUT using a break statement (but i can't figure it out)

import java.io.*;
import java.util.*;

public class Password
{
    public static void main (String [] args)
    {
        final String PASSWORD = "Test";

        for (int passAttempts = 0; passAttempts < 3; passAttempts++) 
        {
            System.out.print("\nEnter Your Password: ");
            Scanner input = new Scanner(System.in);
            String inputPass = input.nextLine();

            if (!(inputPass.equals(PASSWORD))) 
            {
                System.out.println("\nWrong Password Try Again");
            } 
            else 
            {
                System.out.println("\nWelcome!");
                break;
            }
        }
    }
}

This is my alternative attempt of trying to use a switch statement to "catch" each iteration of the loop and perform an action, but I'm not sure why its endless loop:

import java.io.*;
import java.util.*;

public class Password2
{
    public static void main (String [] args)
    {
        final String PASSWORD = "Test";

        System.out.print("\nEnter Your Password: ");
        Scanner input = new Scanner(System.in);
        String inputPass = input.nextLine();

        do
        {
            for(int passAttempts = 0; passAttempts < 3; passAttempts++)
            {
                switch(passAttempts)
                {
                    case 0:
                        System.out.println("\nWrong password try again\t2 attempts remaining");
                        break;
                    case 1:
                        System.out.println("\nWrong password try again\t1 attempts remaining");
                        break;
                    case 2:
                        System.out.println("\nWrong password try again\t0 attempts remaining");
                        break;
                    default:
                        System.out.println("\nContact Help desk");
                        break;
                }  
            }    
        }
        while(!(inputPass.equals(PASSWORD)));
    }
}

Where am I going wrong?

Upvotes: 0

Views: 24081

Answers (4)

user5770672
user5770672

Reputation:

I ended up with the following code in the end after some experimentation. The reasoning behind the programme...does the programme quit because the initialised boolean value of false is changed to true? Or is it because of the value changing in the for loop? Its works but its a bit tricky to explain why it functions hmm...

The for loop to my understanding behaves like so;

  • is 1 <= 3? Yes, so its True...and NOT false = TRUE
  • is 2 <= 3? Yes, so its True...and NOT false = TRUE
  • is 3 <= 3? Yes, so its True...and NOT false = TRUE
  • is 4 <= 3? No, so its false...and NOT false = false -> breaks loop

The part I'm confused on is declaring the boolean value as TRUE when the right password is entered, I don't grasp why it quits the programme...

Can someone clarify this for me with an equation perhaps? Many thanks.

final String PASSWORD = "Test"; //sets constant password 
final int MAX_PASS_ATTEMPTS = 3; //sets constant attempts
boolean adminLoggedIn = false;

    for(int passAttempts = 1; passAttempts <= MAX_PASS_ATTEMPTS && !adminLoggedIn; passAttempts++) //compares and increments passAttempts
    {
        System.out.print("\nPassword: ");
        Scanner input = new Scanner(System.in);
        String inputPass = input.nextLine();

            if (!(inputPass.equals(PASSWORD))) //If "Cisco" is not entered as password
            {
                System.out.println("\nERROR! Wrong Password! \t" + ((passAttempts - MAX_PASS_ATTEMPTS)*(-1)) + " Attempts Remaining"); //Deducts amount of attempts remaining
            } 
            else 
            {
                System.out.println("\nWelcome!");
                adminLoggedIn = true; //breaks loop 
            } 
}

Upvotes: 0

Carlos Ferreira
Carlos Ferreira

Reputation: 432

Just use a while and some flags:

final String PASSWORD = "Test";
    final int MAX_PASS_ATTEMPS = 3;
    boolean isLoggedIn = false;
    int passAttemps = 0;
    while (passAttemps < MAX_PASS_ATTEMPS && !isLoggedIn) {
        System.out.print("\nEnter Your Password: ");
        Scanner input = new Scanner(System.in);
        String inputPass = input.nextLine();

        if (!(inputPass.equals(PASSWORD))) {
        System.out.println("\nWrong Password Try Again");
        passAttemps++;
        } else {
        System.out.println("\nWelcome!");
        isLoggedIn =true;
        break;
        }
    }

Upvotes: 0

user3284549
user3284549

Reputation: 313

You can do it like this, alternatively instead of comparing password in while statement put a break statement below the if(password.equals(PASSWORD), but since you said you want to avoid it:

public static void main(String[] args) 
{
    final String PASSWORD = "Test";
    Scanner sc = new Scanner(System.in);
    int attempts = 3;
    String password = "";
    while (attempts-- > 0 && !PASSWORD.equals(password)) //compares and then decrements
    {
        System.out.print("Enter your password: ");
        password = sc.nextLine();
        if (password.equals(PASSWORD)) 
            System.out.println("Welcome");
        else 
            System.out.println("Incorrect. Number of attempts remaining: " + attempts);        
    }
}

Upvotes: 3

Thiago
Thiago

Reputation: 1

Using flag without change your code:

import java.util.*;

public class Password {
    public static void main(String[] args) {
        final String PASSWORD = "Test";
        boolean wrongPass = true;

        for (int passAttempts = 0; passAttempts < 3 && wrongPass; passAttempts++) {
            System.out.print("\nEnter Your Password: ");
            Scanner input = new Scanner(System.in);
            String inputPass = input.nextLine();

            if (!(inputPass.equals(PASSWORD))) {
                System.out.println("\nWrong Password Try Again");
            } else {
                System.out.println("\nWelcome!");
                wrongPass = false;
            }
        }
    }
}

Ps: close your Scanner ;)

Upvotes: 0

Related Questions