Java BasicsCore· 30 min read

Reading Input with Scanner

The Scanner class lets your program ask the user a question and read their answer.

What you will learn

  • Import and create a Scanner
  • Read text and numbers from the user
  • Build a tiny interactive program

Make your program interactive

So far our programs only talk. To listen to the user, Java gives us the Scanner class. It reads what the user types in the terminal.

Scanner lives in a toolbox called java.util, so first we import it at the top of the file. To import something means to tell Java "I want to use this ready-made tool from elsewhere" — it is like fetching a tool from a shared cupboard before you start, so you can call it by its short name (Scanner) instead of its long full name.

Reading a name and an age from the user
import java.util.Scanner;

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

        System.out.print("What is your name? ");
        String name = input.nextLine();

        System.out.print("How old are you? ");
        int age = input.nextInt();

        System.out.println("Hi " + name + "! Next year you will be " + (age + 1) + ".");
    }
}

Note: Output (the user typed Asha and 25): What is your name? Asha How old are you? 25 Hi Asha! Next year you will be 26. nextLine() reads a whole line of text. nextInt() reads a whole number. We then used age + 1 to do maths on what they typed.

The reading-input flow, step by step

Every interactive program follows the same five steps in order. Here is exactly what the program above did:

  1. Import the Scanner tool at the top: import java.util.Scanner;. Do this once, before the class.
  2. Create a Scanner connected to the keyboard: Scanner input = new Scanner(System.in);. System.in means "input typed in the terminal".
  3. Ask a question with System.out.print(...) so the user knows what to type.
  4. Read their answer with the matching method — nextLine() for text, nextInt() for a whole number — and store it in a variable.
  5. Use the stored value: print it back, do maths with it (like age + 1), or make a decision from it.

The Scanner methods you will use most

MethodReads
nextLine()A whole line of text (a String)
nextInt()A whole number (an int)
nextDouble()A decimal number (a double)
next()A single word (no spaces)

Watch out: A classic Scanner gotcha: after nextInt() or nextDouble(), a following nextLine() can read an empty line left behind. If that happens, add one extra input.nextLine(); to clear it.

Tip: Use System.out.print(...) (not println) before reading input, so the question and the user typing appear on the same line — it feels much more natural.

Q. Which Scanner method reads a whole number that the user types?

Answer: nextInt() reads an int. nextLine() reads text, nextDouble() reads decimals, and there is no nextWord or readInt.

✍️ Practice

  1. Ask the user for two numbers with nextInt() and print their sum.
  2. Ask for the user favourite colour with nextLine() and print a friendly message using it.

🏠 Homework

  1. Build a small program that asks the user for their name and birth year, then prints how old they will turn this year.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →