Reading Console Input with Java's Scanner Class

The Scanner class from java.util provides methods to capture typed input from the console. Several commonly used methods are next(), nextLine(), nextInt(), and nextDouble(). Each input obtained via these methods is treated as a new string when read as text.

next() and nextLine() both return a string representation of the entered data, regardless of its original format. For example, entering abc gives the string "abc", and entering 123 produces the string "123".

Scanner input = new Scanner(System.in);
String val = input.next();
System.out.println(val);
Scanner input = new Scanner(System.in);
String val = input.nextLine();
System.out.println(val);

Internal Behavior

Methods like next(), nextInt(), and nextDouble() stop consuming input when they encounter a whitespace character (space, tab, or newline). That whitespace and any remaining input are left in the buffer. For instance:

Scanner input = new Scanner(System.in);
double d = input.nextDouble();
System.out.println(d);
// Input: 3.14 2.71
// Output: 3.14

The nextDouble() call stops at the space after 3.14. The space and 2.71 stay in the buffer and can be picked up by a subsequent input call.

Scanner input = new Scanner(System.in);
String first = input.next();
String second = input.next();
System.out.println(first);
System.out.println(second);
// Input: hello world
// first receives "hello", second receives "world"

After the first next() reads "hello", the space and "world" remain. The second next() discards leading whitespace and reads "world".

In contrast, nextLine() reads the entire line, includign leading and trailing spaces, until a newline character is encountered.

Scanner input = new Scanner(System.in);
String line = input.nextLine();
System.out.println(line);
// Input:  hello there  
// Output: " hello there  "

Mixing Input Methods

Combining nextInt() (or next(), nextDouble()) with nextLine() often leads to unexpected behavior. Consider the following:

Scanner input = new Scanner(System.in);
int number = input.nextInt();
String text = input.nextLine();
System.out.println(number);
System.out.println(text);

When the user enters 123 and presses Enter, the input stream contains "123\n". The nextInt() call extracts 123 but leaves the newline character in the buffer. Consequently, nextLine() reads that leftover newline immediately, returning an empty string. The variable text does not wait for new user input.

Recommended Usage Patterns

Avoid mixing token-based methods (such as next(), nextInt(), nextDouble()) with nextLine(). Two consistent approaches are:

  • Use next(), nextInt(), and nextDouble() together. After reading a token, if a line is needed, use only the token‑based methods and avoid nextLine().
Scanner input = new Scanner(System.in);
String token = input.next();   // e.g., user enters 42
System.out.println("String: " + token);
int num = input.nextInt();     // next entry as integer
System.out.println("Integer: " + num);
  • Rely exclusively on nextLine(). If a numeric value is required, parse the line into the desired type.
Scanner input = new Scanner(System.in);
String raw = input.nextLine();
int value = Integer.parseInt(raw);
System.out.println("Integer: " + value);

By sticking to one approach, the buffer handling remains predictable and avoids skipped inputs.

Tags: java Scanner Console input I/O

Posted on Mon, 29 Jun 2026 17:07:04 +0000 by TronB24