Java User Input (Scanner)

User input means data typed by the person running a program. In Java, the Scanner class can read input from the keyboard and store it in variables.

Input makes a program respond to the user instead of always using fixed values. A common pattern is to print a prompt, read a value, then use that value in the program.

Import Scanner

Scanner is in the java.util package, so programs that use it usually start with import java.util.Scanner;.

To read from the keyboard, create a scanner with new Scanner(System.in). The System.in object represents standard input, which is normally the keyboard.

import java.util.Scanner;

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

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

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

Output:

Enter your age: You are 18 years old.

This sample output assumes the user typed 18 and pressed Enter.

How It Works

The line Scanner input = new Scanner(System.in); creates a Scanner object named input. You can choose another variable name, but input is clear in beginner examples.

The statement input.nextInt() waits for the user to type a whole number. When the user enters 18, Java stores that number in the age variable.

The type you read should match the type of data you expect. Use nextInt() for an int, nextDouble() for a decimal number, and string methods such as next() or nextLine() for text.

Reading Different Types

A scanner has different methods for different kinds of input.

Method Reads
nextInt() A whole number, such as 42.
nextDouble() A decimal number, such as 19.99.
next() One word of text, stopping at whitespace.
nextLine() A whole line of text, including spaces.
import java.util.Scanner;

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

        System.out.print("Enter item: ");
        String item = input.next();

        System.out.print("Enter price: ");
        double price = input.nextDouble();

        System.out.println(item + " costs $" + price);
        input.close();
    }
}

Output:

Enter item: Enter price: notebook costs $2.5

This sample output assumes the user typed notebook, pressed Enter, typed 2.5, and pressed Enter again.

Reading A Full Line

Use nextLine() when the input may contain spaces, such as a full name or sentence. The next() method would only read the first word.

import java.util.Scanner;

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

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

        System.out.println("Welcome, " + fullName + ".");
        input.close();
    }
}

Output:

Enter your full name: Welcome, Ada Lovelace.

This sample output assumes the user typed Ada Lovelace and pressed Enter.

Mixing Numbers And Lines

One common beginner mistake happens when nextInt() or nextDouble() is followed by nextLine(). The numeric method reads the number, but it leaves the Enter key press behind. A following nextLine() may read that leftover line break.

A simple fix is to call nextLine() once after the number, before reading the real line of text.

import java.util.Scanner;

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

        System.out.print("Enter year: ");
        int year = input.nextInt();
        input.nextLine();

        System.out.print("Enter course name: ");
        String course = input.nextLine();

        System.out.println(course + " starts in " + year + ".");
        input.close();
    }
}

Output:

Enter year: Enter course name: Java Basics starts in 2026.

This sample output assumes the user typed 2026, pressed Enter, typed Java Basics, and pressed Enter again.

Common Beginner Mistakes

  • Forgetting import java.util.Scanner; before using Scanner.
  • Using next() when the input may contain spaces. Use nextLine() for a full line.
  • Typing text when the program expects a number, such as using letters with nextInt().
  • Forgetting that a scanner waits until the user types input and presses Enter.
  • Mixing numeric input and line input without handling the leftover line break.

Takeaway: use Scanner with System.in to read typed values, choose the method that matches the expected data, and use nextLine() for full lines of text.