When you read in the year month day hour minutes with something like nextInt() it leaves rest of the line in the parser/buffer (even if it is blank) so when you call nextLine() you are reading the rest of this first line.
I suggest you call scan.nextLine() before you print your next prompt to discard the rest of the line.
Answer from Peter Lawrey on Stack OverflowI have a problem reading string input using the scanner in java - Stack Overflow
java.util.scanner - How can I read input from the console using the Scanner class in Java? - Stack Overflow
Scanner Not Working in a While Loop
How do I get multiple words of input from a user to store in one variable?
What’s the best way to store multiple string inputs in Java for later use?
How do you handle exceptions or errors while taking multiple string inputs using Scanner?
How can I efficiently store multiple string inputs read by Scanner for further processing?
When you read in the year month day hour minutes with something like nextInt() it leaves rest of the line in the parser/buffer (even if it is blank) so when you call nextLine() you are reading the rest of this first line.
I suggest you call scan.nextLine() before you print your next prompt to discard the rest of the line.
Scanner ss = new Scanner(System.in);
System.out.print("Enter the your Name : ");
// Below Statement used for getting String including sentence
String s = ss.nextLine();
// Below Statement used for return the first word in the sentence
String s = ss.next();
Thankful to help. This error occurs because the nextInt() and nextDouble() methods don't read the newline characters of your input.
You can easily fix this either by parsing int from nextLine(): Integer.parseInt(scan.nextLine()) or simply using the InputStreamReader class as:
//sample code for understanding
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader read=new BufferedReader(in);
//taking input
System.out.print("Enter a number: ");
int val=Integer.parseInt(read.readLine());
System.out.println("Value entered: "+val);
The short of it? Don't call nextLine - it is confusing in how it operates.
If you want to read strings, use next() instead. If you want an entire line's worth, instead of a single word, update your scanner to work in 'line mode' instead of 'space mode':
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\r?\n");
// and now use as normal:
int i = scan.nextInt();
double d=scan.nextDouble();
String s=scan.next();
That sets the scanner to scan up to newline characters, which are a little convoluted; on windows they are \r\n, but on other OSes they are just \n, hence why we specify: optional \r, then a required \n.
A simple example to illustrate how java.util.Scanner works would be reading a single integer from System.in. It's really quite simple.
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
To retrieve a username I would probably use sc.nextLine().
System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
System.out.println("Your username is " + username);
You could also use next(String pattern) if you want more control over the input, or just validate the username variable.
You'll find more information on their implementation in the API Documentation for java.util.Scanner
Scanner scan = new Scanner(System.in);
String myLine = scan.nextLine();