0Pricing
Java Academy · Lesson

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

  1. Reading Input with Scanner
  2. Reading Numbers and Lines
  3. Validating User Input
  4. BufferedReader Alternative
← Back to Java Academy