Imagine the following scanario: I have a program which ask for an integer input, followed by a String input.
int age=0;
String name;
Scanner sc = new Scanne
@skiwi is right about only using one Scanner
, so you're doing that right. The reason it doesn't work is that nextInt()
consumes all characters that make up the integer, but it does not touch the end-of-line character. So when nextLine()
is called, it sees that there are no characters before the end-of-line character, so it thinks that an empty line was entered, and you get an empty String back. However, nextLine()
does consume the end-of-line character, so if you call sc.nextLine();
once before you do name = sc.nextLine();
, it should work.