0

I'm not sure how dumb I am or if I am missing something quite simple; anyhow, I am trying to get a basic username and password input from the user using the scanner utility.

I have made sure scanner was initialised correctly (Scanner sc = new Scanner(System.in)

System.out.print("Username or Email: ");
String username = sc.nextLine();
System.out.print("\nPassword: ");
String password = sc.nextLine();

The issue I am having is when I run this part of the code (above), the output I get looks like this (below) where I start inputting into the password section. Its as though it is just skipping over the first call to the scanner.

Username or Email: 
Password: (this is where my input goes)

My only guess is that the scanner is taking the second printing as its input but I am not sure so any help is greatly appreciated.

p.s I will leave the entire method at the bottom incase it helps.

Thanks.

public static void loginPage() throws SQLException{
    int requestCounter = 0;
    do {
        System.out.print("Username or Email: ");
        String username = sc.nextLine();
        System.out.print("\nPassword: ");
        String password = sc.nextLine();
        boolean validLoginRequest = accountLoginCheck(username, password);
        if (validLoginRequest) {
            break;
        } else {
            requestCounter++;
        }
    } while (requestCounter < 3);
    if (requestCounter == 3) {
        System.out.println("Too many attempts");
        return;
    }
    System.out.print("TO MAIN MENU");
  • 1
    Before the first call to `nextLine` you have shown here, did you call another `nextXXX` method? – Sweeper Mar 21 '19 at 07:09
  • Yes, prior to is a basic command line menu where the user inputs a number to move to the next "page" for lack of a better word. { System.out.print("\nWhat would you like to do?: "); int userInput = sc.nextInt(); } – Nathan Winspear Mar 21 '19 at 07:30

1 Answers1

0

Remove \n from System.out.print("\nPassword: ");

Username or Email: myemail
Password: pass
  • still gives me the same issue just, password isn't printed on a new line now. `Username or Email: Password:` – Nathan Winspear Mar 21 '19 at 07:34
  • It seems to be that where ever you are calling `loginPage()` from, there is something in input buffer already present. Can you share the code of caller of `loginPage()` – Vinay Avasthi Mar 21 '19 at 08:37