0

I'm fairly new to Java and I'm facing some difficulties. So i've been instructed to run a program where you will login in to a system by entering a pin number and school name. You have 3 attempts for each until a message prompts that tells you that the login has failed. My problem is. Everything is fine but in PIN SECTION, (userInputPin==PIN) section, it automatically inputs "Attempt #2 - Enter your school name - Incorrect. upon first correct attempt. When writing the correct school name, it shows login failed as well when it should notify that you have logged in. What's the error?

Note:Ignore comment, I'll fix them.

public class Login {

    public static final int PIN = 1234; //Declaring constant for fixed PIN

    //Declaring constant for first school name
    public static final String FIRST_SCHOOL = "St. Charles"; 

    public static void main(String[] args) {


        Scanner kb = new Scanner (System.in); //Declaring scanner object

        int attempts = 1; //Declaring variable for attempt number

        //Printing first paragraph section of the program
        System.out.println("This program simulates logging into a bank account,"
                + "\nasking certain questions for security.\n");

        // PIN Section
        while(attempts<=3) //While loop
        {
          System.out.print("Attempt #"+attempts+" - Enter PIN: "); 
          int userInputPin = kb.nextInt(); //User inputs pin number

          //Conditional situations
          if(userInputPin==PIN)
          {
              attempts=1;
              while(attempts<=3)
        {
            System.out.print("\nAttempt #"+ attempts+" - Enter your first school: ");
            String userInputSchool = kb.next();

            //Conditional situations
            if(userInputSchool.equals(FIRST_SCHOOL))
                {
                System.out.println("\nYou're logged in.");
                }
            else{
                if(attempts==3)
                {
                    System.out.println("\nLogin failed.");
                }
                else
                {
                    System.out.println("Incorrect.\n");
                }
            }
           attempts++;
        }

          }
          else{
              if(attempts==3){
                  System.out.println("\nLogin failed.");
              }
              else{
                  System.out.println("Incorrect.\n");
              }
          }
        attempts++; //Increments attempt by 1 when PIN is incorrect          
        }
Skyze
  • 9
  • 2
  • I've submitted an answer, but if you feel you want more in depth knowledge of how scanner works, I suggest you take a look at https://stackoverflow.com/questions/26446599/how-to-use-java-util-scanner-to-correctly-read-user-input-from-system-in-and-act – Robo Mop Feb 13 '20 at 17:54
  • related [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/85421) – user85421 Feb 13 '20 at 18:04

1 Answers1

1

Ah yes, ye ol' Scanner. I can't begin to tell you how many times I've suffered the same problem.

The problem lies in the fact that the nextInt() function sometimes regards the enter key as another token. So when you input the first value, nextInt() recognizes the number inputted. But after printing the second message, the scanner object still has the enter key stored in it. The only way to move forward is to empty the object like so:

if(kb.hasNext()) kb.nextLine();

Insert this after each time you input a number.

Robo Mop
  • 2,958
  • 1
  • 7
  • 23