Using scanner.nextLine ()

I am having trouble trying to use the nextLine () method from java.util.Scanner.

Here is what I tried:

import java.util.Scanner; class TestRevised { public void menu() { Scanner scanner = new Scanner(System.in); System.out.print("Enter a sentence:\t"); String sentence = scanner.nextLine(); System.out.print("Enter an index:\t"); int index = scanner.nextInt(); System.out.println("\nYour sentence:\t" + sentence); System.out.println("Your index:\t" + index); } } 

Example # 1: This example works as intended. String String sentence = scanner.nextLine(); waits for input before continuing System.out.print("Enter an index:\t"); .

This leads to the output:

 Enter a sentence: Hello. Enter an index: 0 Your sentence: Hello. Your index: 0 



 // Example #2 import java.util.Scanner; class Test { public void menu() { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("\nMenu Options\n"); System.out.println("(1) - do this"); System.out.println("(2) - quit"); System.out.print("Please enter your selection:\t"); int selection = scanner.nextInt(); if (selection == 1) { System.out.print("Enter a sentence:\t"); String sentence = scanner.nextLine(); System.out.print("Enter an index:\t"); int index = scanner.nextInt(); System.out.println("\nYour sentence:\t" + sentence); System.out.println("Your index:\t" + index); } else if (selection == 2) { break; } } } } 

Example # 2: This example does not work properly. This example uses a while and if - else loop to let the user choose what to do. As soon as the program reaches String sentence = scanner.nextLine(); , it does not wait for input, but instead executes the line System.out.print("Enter an index:\t"); .

This leads to the output:

 Menu Options (1) - do this (2) - quit Please enter your selection: 1 Enter a sentence: Enter an index: 

This makes it impossible to enter a sentence.




Why does example # 2 not work as intended? The only difference between Ex. 1 and 2 are an example. 2 has a while loop and an if-else structure. I do not understand why this affects the behavior of the .nextInt () scanner.

+81
java java.util.scanner
Feb 17 2018-11-17T00:
source share
5 answers

I think your problem is that

 int selection = scanner.nextInt(); 

reads only the number, not the end of the line or anything after the number. When you announce

 String sentence = scanner.nextLine(); 

This reads the remainder of the line with the number on it (nothing after the number I suspect)

Try placing the scanner .nextLine (); after each nextInt (), if you intend to ignore the rest of the line.

+114
Feb 17 2018-11-17T00:
source share

Instead of placing an extra scanner.nextLine() every time you want to read something, since it seems like you want to accept every input on a new line, you can instead change the delimiter to actually match only newline characters (instead of any space, as by default)

 import java.util.Scanner; class ScannerTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); scanner.useDelimiter("\\n"); System.out.print("Enter an index: "); int index = scanner.nextInt(); System.out.print("Enter a sentence: "); String sentence = scanner.next(); System.out.println("\nYour sentence: " + sentence); System.out.println("Your index: " + index); } } 

Thus, to read the input line, you only need scanner.next() , which has the same behavior with the delimiter of the following {Int, Double, ...}

The difference with the nextLine () approach each time is that the latter will also take <space>3 , 3<space> and 3<space>whatever as the index, while the former will only accept 3 in a row on its own

+21
Jun 13 '13 at 10:19
source share

Do not try to scan text with nextLine (); AFTER using nextInt () with the same scanner! This does not work very well with the Java Scanner, and many Java developers prefer to use a different scanner for integers. You can call these scanners scan1 and scan2 if you want.

+10
Sep 17 '13 at 9:02
source share

This is because when you enter a number, then press Enter, input.nextInt() only consumes the number, not the "end of line". Primitive data types, such as int, double, etc., do not consume "end of line", therefore, "end of line" remains in the buffer, and when input.next() is executed, it consumes "end of line" from the buffer from the first entrance. Therefore, your String sentence = scanner.next() only consumes the "end of line" and does not wait to read from the keyboard.

Tip: use scanner.nextLine() instead of scanner.next() because scanner.next() does not read spaces from the keyboard. (Truncate the line after providing some space from the keyboard, only show the line before the space.)

+8
Jul 17 '13 at 9:31 on
source share

or

 int selection = Integer.parseInt(scanner.nextLine()); 
+3
Jul 13 '13 at 6:32
source share



All Articles