Sub topic 2
Creating a Scanner
Prerequisites
This chapter assumes you know basic Java syntax, including classes, methods, variables, and if statements. You should also know how to compile and run a Java program and recognize types such as String, int, and double.
A Java console program reads input from standard input, the stream of data provided to the program. Java exposes standard input through System.in. A Scanner is an object from java.util that reads and converts data from an input stream.
Constructing a Scanner
Import Scanner, then pass System.in to its constructor:
import java.util.Scanner;
public class Greeter {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Name: ");
String name = in.nextLine();
System.out.println("Hello, " + name);
}
}The statement new Scanner(System.in) creates a Scanner connected to the keyboard input for this program. The variable in stores a reference to that object. You can choose another variable name, such as scanner, but a short name like in is common for a scanner reading standard input.
The call System.out.print displays a prompt without moving to a new line. The program then waits at in.nextLine() until the user enters a line and presses Enter.
The data path looks like this:
What the Scanner Reads
A scanner can read different Java types. Each method reads the next matching part of the input.
| Method | Result | Typical input |
|---|---|---|
| `next()` | One word as a `String` | `Ada` |
| `nextInt()` | One integer as an `int` | `42` |
| `nextDouble()` | One decimal number as a `double` | `19.5` |
| `nextLine()` | The rest of the current line as a `String` | `Ada Lovelace` |
For example:
import java.util.Scanner;
public class Profile {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("First name: ");
String firstName = in.next();
System.out.print("Age: ");
int age = in.nextInt();
System.out.print("Height in meters: ");
double height = in.nextDouble();
System.out.println(firstName + " is " + age
+ " years old and " + height + " meters tall.");
}
}When this program uses next(), it reads only one word. If the user enters Ada Lovelace, firstName becomes "Ada" and Lovelace remains in the scanner's input. When nextInt() runs, it looks for the next token and attempts to convert it to an integer.
A token is a piece of input separated by whitespace, such as a space, tab, or newline. By default, a scanner skips whitespace before reading a token. This behavior makes methods such as nextInt() useful for sequences of numbers.
Choosing the Initial Read
Use nextLine() when the user may enter spaces and the whole response belongs together, such as a name, title, or sentence. Use token methods when the input is naturally separated into individual values.
Do not create a new scanner for every question. One scanner connected to System.in can read the program's input from start to finish. Also avoid closing that scanner while the program still needs console input. Closing it closes its underlying input stream, including System.in.
Exercise
Which scanner method should you use to read the complete response New York, including the space?
Show answer
Use nextLine(). The next() method would read only New.