Reading Input with Scanner
Read user input from System.in.
Reading From the Keyboard
To read what a user types, Java provides the Scanner class in the java.util package. You attach it to System.in, the standard input stream.
Scanner makes reading words, lines, and numbers simple.
Creating a Scanner
Construct a Scanner by passing System.in. The examples here use fixed strings so they run without input, but the same methods apply to keyboard input.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner("Hello");
String word = scanner.next();
System.out.println(word);
scanner.close();
}
}All lessons in this course
- Reading Input with Scanner
- Reading Numbers and Lines
- Validating User Input
- BufferedReader Alternative