01 / 20

Scanner for
Console Input

Learn how to read user input from the console using Java's Scanner class: the essential gateway to interactive programs.

Java

What Is Console Input?

Most programs need to receive data from the user while they run. In Java, the simplest way to do this is through the console: the text-based terminal where you see program output and type responses.

Without input, every program would produce the same output every time. Input makes programs dynamic and interactive.

Introducing Scanner

Java provides a built-in class called Scanner in the java.util package. It acts as a bridge between the keyboard and your program's variables.

Keyboard System.in Scanner parses tokens User types Raw byte stream Typed values (int, String ...)

Why Use Scanner?

★ Simple
Only 2 lines of setup: one import and one constructor call.
★ Versatile
Read integers, decimals, booleans, full lines, and more.
★ Built-in
Part of the standard library: no external downloads needed.

For AP Computer Science and most introductory Java courses, Scanner is the standard way to handle console input.

The Two Required Lines

Every program that uses Scanner needs exactly two setup steps before reading any input:

Java
// Step 1: Import the Scanner class
import java.util.Scanner;

public class MyProgram {
    public static void main(String[] args) {
        // Step 2: Create a Scanner object
        Scanner input = new Scanner(System.in);
    }
}

Core Scanner Methods

Method Return Type Reads
nextInt() int One integer
nextDouble() double One decimal number
next() String One word (no spaces)
nextLine() String Entire line (with spaces)
nextBoolean() boolean true or false

next() vs nextLine()

This is the most important distinction for beginners. Both read String values, but they behave very differently:

next()
Reads up to the first whitespace. If the user types "Hello World", it returns only "Hello".
nextLine()
Reads the entire line until Enter is pressed. "Hello World" returns "Hello World".

Step-by-Step Approach

1
Import. Add import java.util.Scanner; at the top of your file.
2
Create. Declare Scanner sc = new Scanner(System.in); inside your method.
3
Prompt. Use System.out.print() to tell the user what to type.
4
Read. Call the appropriate method: nextInt(), nextLine(), etc.
5
Use. Store the result in a variable and process it.
6
Close. Call sc.close(); when finished to release resources.

Example 1: Reading an Integer

Java
import java.util.Scanner;

public class AgeChecker {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your age: ");
        int age = sc.nextInt();

        System.out.println("You are " + age + " years old.");
        sc.close();
    }
}

Example 2: Reading a Full Name

Java
import java.util.Scanner;

public class Greeter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your full name: ");
        String name = sc.nextLine();

        System.out.println("Hello, " + name + "!");
        sc.close();
    }
}

Example 3: Multiple Inputs

Java
import java.util.Scanner;

public class RectangleArea {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Width: ");
        double w = sc.nextDouble();

        System.out.print("Height: ");
        double h = sc.nextDouble();

        System.out.println("Area = " + (w * h));
        sc.close();
    }
}

The nextLine() Trap

The most common Scanner bug happens when you mix nextInt() (or nextDouble()) with nextLine():

Buggy Code
System.out.print("Age: ");
int age = sc.nextInt();       // reads "25" but leaves \n

System.out.print("Name: ");
String name = sc.nextLine();  // reads the leftover \n!
// name is now "" (empty string)
Fixed
int age = sc.nextInt();
sc.nextLine();  // consume leftover newline
String name = sc.nextLine();  // now reads correctly

InputMismatchException

If you call nextInt() but the user types a word like "hello", Java throws an InputMismatchException and your program crashes.

User types: "abc" nextInt() InputMismatchException

Common Mistakes

✕ Forgetting the Import
Without import java.util.Scanner; the compiler cannot find the Scanner class.
✕ Wrong Method
Using next() when the input has spaces. Only nextLine() captures the full line.
✕ The Newline Bug
Calling nextLine() right after nextInt() without consuming the leftover \n.

Method Summary

I need to read... Use this method Watch out for
A whole number nextInt() Leftover \n
A decimal nextDouble() Leftover \n
A single word next() Stops at spaces
A full sentence nextLine() Mixing with nextInt()
true / false nextBoolean() Case-insensitive

Question 1 of 5

Which import statement is required before using the Scanner class?

Question 2 of 5

What does nextInt() return when the user types "42" and presses Enter?

Question 3 of 5

A user types "Hello World". Which method captures the entire input including the space?

Question 4 of 5

What value does the variable s hold after the code runs?

Question 5 of 5

What happens if you call nextInt() and the user types "abc"?