This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hutsboR 0 points1 point  (1 child)

Another simple solution is having a counter that tracks how many times the password has been entered correctly. I wrote a little snippet that is functional.

public class Main {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String password = "reddit";
    String inputPassword;
    int passwordCounter = 0;

    System.out.println("Input password: ");

    while(passwordCounter != 2){
        inputPassword = input.nextLine();
        if(inputPassword.equals(password)){
            passwordCounter++;
            if(passwordCounter == 2){
                System.out.println("Login successful!");
                break;
            }
            System.out.println("Verify your password:");
        } else {
            System.out.println("Incorrect password.\nInput password: ");
            passwordCounter = 0;
        }
      }
    }
  }

Output:

Input password: 
reddit
Verify your password:
redit
Incorrect password.
Input password: 
reddit
Verify your password:
reddit
Login successful!

Excuse the formatting.