Yaz
Yaz

Reputation: 55

Looping code after incorrect user input entry

So I've got a very basic and simple code running at the moment which prompts the user to enter a username, password and account number which is then compared against a hardcoded username, password and account number. I am looking for a way to add the code to a loop so that the user is given THREE chances to enter the username, password and account number correctly (so that it matches the hardcoded ones) before the program terminates. If they enter them wrong THREE times, the program will terminate. Could anyone help me out please, I am a little stuck. My code currently compares the user input with the hardcoded variables but terminates after one incorrect entry.

Thanks

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

public class Program4 {

static Scanner console = new Scanner(System.in);

public static void main(String[] args) {

    final int Username = 3387;
    final int Password = 5183;
    final int AccountNumber = 22334455;

    final int EnteredUsername;
    System.out.println("Enter Username");
    EnteredUsername = console.nextInt();
    System.out.println("Username Entered is " + EnteredUsername);

    System.out.printf("%10s\n", " "); //Ignore these lines, they are just to make the console easier for me to read.

    final int EnteredPassword;
    System.out.println("Enter Password");
    EnteredPassword = console.nextInt();
    System.out.println("Password Entered is " + EnteredPassword);

    System.out.printf("%10s\n", " ");

    final int EnteredAccountNumber;
    System.out.println("Enter Account Number");
    EnteredAccountNumber = console.nextInt();
    System.out.println("Account Number Entered is " + EnteredAccountNumber);

    System.out.printf("%10s\n", " ");

    if (Username == EnteredUsername && (Password == EnteredPassword)
            && (AccountNumber == EnteredAccountNumber)) {

        System.out.println("Authentication Complete!");

    } else {
        System.out.println("Wrong Username, Password or Account Number. Please try again");
        System.exit(0);
    }

}

}

Upvotes: 0

Views: 4710

Answers (3)

Juned Ahsan
Juned Ahsan

Reputation: 68715

Use a do while loop with a counter and a flag. Counter should be used for the 3 tries whereas flag should be used to terminate the loop when entries are valid. Here is a sample:

  int attempts = 0; // increment this when wrong crendentials
  boolean authenitcated = false; // set to true when credentials are correct
  do { 
     // your code
   }while(attempts < 3 && authenticated == false)

EDIT: Try this

public static void main(String[] args) {

    final int Username = 3387;
    final int Password = 5183;
    final int AccountNumber = 22334455;

 int attempts = 0;
  boolean authenitcated = false;
  do { 
  final int EnteredUsername;
    System.out.println("Enter Username");
    EnteredUsername = console.nextInt();
    System.out.println("Username Entered is " + EnteredUsername);

    System.out.printf("%10s\n", " "); //Ignore these lines, they are just to make the console easier for me to read.

    final int EnteredPassword;
    System.out.println("Enter Password");
    EnteredPassword = console.nextInt();
    System.out.println("Password Entered is " + EnteredPassword);

    System.out.printf("%10s\n", " ");

    final int EnteredAccountNumber;
    System.out.println("Enter Account Number");
    EnteredAccountNumber = console.nextInt();
    System.out.println("Account Number Entered is " + EnteredAccountNumber);

    System.out.printf("%10s\n", " ");

    if (Username == EnteredUsername && (Password == EnteredPassword)
            && (AccountNumber == EnteredAccountNumber)) {

        System.out.println("Authentication Complete!");
        authenitcated = true;

    } else {
        System.out.println("Wrong Username, Password or Account Number. Please try again");
        attempts ++;

    }
   }while(attempts < 3 && authenticated == false)

}

Upvotes: 1

user3024094
user3024094

Reputation: 26

import java.util.Scanner;

public class StackFlow {




    public static void main(String[] args) {
      Scanner console = new Scanner(System.in);

        final int Username = 3387;
        final int Password = 5183;
        final int AccountNumber = 22334455;

         int EnteredUsername;
         int EnteredPassword;
         int EnteredAccountNumber;
        for(int s=0;s<=3;s++)
        {if (s<3)
        {System.out.println("Enter Username");
        EnteredUsername = console.nextInt();
        System.out.println("Username Entered is " + EnteredUsername);
        System.out.println("Enter Password");
        EnteredPassword = console.nextInt();
        System.out.println("Password Entered is " + EnteredPassword);
        System.out.println("Enter Account Number");
        EnteredAccountNumber = console.nextInt();
        System.out.println("Account Number Entered is " + EnteredAccountNumber);
        if (Username == EnteredUsername && (Password == EnteredPassword)
                && (AccountNumber == EnteredAccountNumber)) {
            System.out.println("Welcome");
            break;
        }
        else {
            System.out.println("Wrong Username, Password or Account Number. Please try again");
        }
        }
            else
        {
            System.out.println("You waste your 3 chances Bye-Bye");
        }
    }

}}

Upvotes: 0

sanket
sanket

Reputation: 789

Define a counter. Increment the counter in your else block. Check if the counter = 3, only then exit.

Upvotes: 0

Related Questions