To get user input in Java, we use the Scanner class, which is found in the java.util package. It allows your program to "wait" for the user to type something into the console and then capture that data to use in your logic. Whether it's a name, an age, or a decimal value, the Scanner class has a specialized method for every data type.
In this tutorial, we will cover:
To use the Scanner, you must follow three essential steps:
import java.util.Scanner;.System.in).Depending on the type of data you expect from the user, you must choose the correct method. Using the wrong method (e.g., trying to read a String into an Integer variable) will result in an InputMismatchException.
| Method | Data Type | Description |
|---|---|---|
next() |
String | Reads a single word (stops at space). |
nextLine() |
String | Reads a complete line (including spaces). |
nextInt() |
int | Reads a whole number. |
nextDouble() |
double | Reads a decimal number. |
nextBoolean() |
boolean | Reads 'true' or 'false'. |
A very common issue occurs when you use nextLine() immediately after nextInt(). The nextInt() method only reads the number and leaves the "Enter" key (newline character) in the buffer. When nextLine() runs, it sees that "Enter" key and thinks the user has already finished typing!
scanner.nextLine(); after reading a number to "clear the buffer" before reading a real String.
Once you are finished taking input, it is considered best practice to close the scanner using myObj.close();. This releases the system resources. In small programs, it doesn't matter much, but in professional, long-running applications, failing to close resources can lead to **Memory Leaks**.
This program demonstrates how to read different types of data and handle the buffer correctly.
What if the user types "Hello" when you expected an Integer? Your program will crash. To prevent this, professional developers use hasNextInt() or hasNextDouble() to check the input before reading it.
if (input.hasNextInt()) {
int val = input.nextInt();
} else {
System.out.println("Error: Please enter a valid number.");
}
Q: What is the difference between next() and nextLine()?
A: next() reads input until it finds a space (a single word). nextLine() reads until it finds a line break (the entire line).
Q: Can Scanner read from a file?
A: Yes! Instead of passing System.in, you can pass a File object to the Scanner constructor to read text files line by line.
Q: Why is Scanner preferred over BufferedReader for beginners?
A: Scanner automatically parses primitives (like int, double), whereas BufferedReader reads everything as a String, requiring the developer to manually convert types using Integer.parseInt().
The Scanner class is your bridge to the user. By understanding how to manage its buffer and choosing the right methods for each data type, you can build interactive tools, games, and calculators. Always remember to validate your input to make your applications "bulletproof" against user errors.
Next: Working with Dates and Times →