The Scanner Class
The java.util.Scanner class is one of the most frequently used classes by beginners learning Java. It provides a simple and intuitive way to read input from various sources, most commonly from the user via the console.
Under the hood, the Scanner class breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
1. Reading from the Console
To read user input from the console, you create a Scanner object and pass System.in (the standard input stream) to its constructor.
import java.util.Scanner;
public class LabScanner1 {
public static void main(String args[]) {
// Creating the Scanner object attached to the console
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name:");
// Reading a String (entire line)
String name = scanner.nextLine();
System.out.println("Welcome, " + name + "!");
// It is best practice to close the scanner when you are done!
scanner.close();
}
}
2. Parsing Different Data Types
The Scanner class provides numerous methods to read and parse different primitive data types directly.
| Method | Description |
|---|---|
nextLine() | Reads an entire line of text (until it hits the enter key). |
next() | Reads a single word (until the first whitespace). |
nextInt() | Reads an int value. |
nextDouble() | Reads a double value. |
nextBoolean() | Reads a boolean value (true or false). |
nextLong() | Reads a long value. |
Example: Reading Multiple Data Types
import java.util.Scanner;
public class LabScanner2 {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your Age (integer):");
int age = scanner.nextInt();
System.out.println("Enter your Salary (decimal):");
double salary = scanner.nextDouble();
System.out.println("Are you employed? (true/false):");
boolean isEmployed = scanner.nextBoolean();
System.out.println("\n--- User Profile ---");
System.out.println("Age: " + age);
System.out.println("Salary: $" + salary);
System.out.println("Employed: " + isEmployed);
scanner.close();
}
}
3. The nextLine() Gotcha!
One of the most common issues beginners face with the Scanner class is mixing nextInt() or nextDouble() with nextLine().
When you type a number and press "Enter", you are actually sending the number and a newline character (\n) to the scanner. The nextInt() method reads the number but leaves the \n character in the buffer.
If you immediately call nextLine() after nextInt(), the nextLine() method reads that leftover \n character and immediately finishes without letting the user type anything!
The Solution
Always add an extra, empty scanner.nextLine() call after reading primitives to consume the leftover newline character.
import java.util.Scanner;
public class LabScanner3 {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Age:");
int age = scanner.nextInt();
// CONSUME THE LEFTOVER NEWLINE CHARACTER!
scanner.nextLine();
System.out.println("Enter Name:");
String name = scanner.nextLine();
System.out.println("Name: " + name + ", Age: " + age);
scanner.close();
}
}