0

so I am currently having difficult trying to fix this. In short, I'm trying to create a banking application for a class. I'm writing a method to print and get user's inputs but the print lines print out both lines before I can get inputs. Here is my implementation.

boolean done = false; 
        while(!done) {
            System.out.println("Welcome to Running Bank"
                        + "\nWhat would you like to do?"
                        + "\n1-Create Account"
                        + "\n2-Log-In"
                        + "\n3-Exit");
            choice = scanner.nextInt();
            switch(choice) {
            case 1:
                System.out.println("Input your username(No more than 15 characters: ");
                Username = scanner.nextLine();
                System.out.println("Input your password(Must be at least 8 characters and not over 24): ");
                Password = scanner.nextLine();
        }
    }
}

What I expected it to do was to print out "Input your username" then after I get the user's input for it then to print the next line. However, what happening right now is that both lines get print before I can put in anything. Thank you in advanced.

smac89
  • 26,360
  • 11
  • 91
  • 124
  • 3
    Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Johannes Kuhn Nov 20 '19 at 04:04
  • Put `scanner.nextLine()` right after `choice = scanner.nextInt()` (as explained in the post Johannes Kuhn put in the comment above) – Skylar Nov 20 '19 at 04:09

1 Answers1

0
boolean done = false; 
        while(!done) {
            System.out.println("Welcome to Running Bank"
                        + "\nWhat would you like to do?"
                        + "\n1-Create Account"
                        + "\n2-Log-In"
                        + "\n3-Exit");
            choice = scanner.nextInt();
            scanner.nextLine();
            switch(choice) {
            case 1:
                System.out.println("Input your username(No more than 15 characters: ");
                Username = scanner.nextLine();
                System.out.println("Input your password(Must be at least 8 characters and not over 24): ");
                Password = scanner.nextLine();
        }
    }
}

This should work now. I have added a scanner.nextLine() after inputing choice

thisisjaymehta
  • 600
  • 1
  • 8
  • 21