0Pricing
Java Academy · Lesson

Reading Input with Scanner

Read user input from System.in.

Reading Input with Scanner is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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();
    }
}

Reading a Single Word

next() reads one token, meaning text up to the next whitespace. Spaces and newlines separate tokens.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner("apple banana cherry");
        System.out.println(scanner.next());
        System.out.println(scanner.next());
        scanner.close();
    }
}

Reading a Full Line

nextLine() reads everything up to the end of the line, including spaces. Use it when the input contains multiple words you want together.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner("John Smith");
        String fullName = scanner.nextLine();
        System.out.println("Name: " + fullName);
        scanner.close();
    }
}

Checking for More Input

hasNext() returns true while there are more tokens. This lets you loop until the input is exhausted.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner("red green blue");
        while (scanner.hasNext()) {
            System.out.println(scanner.next());
        }
        scanner.close();
    }
}

Reading Line by Line

Pair hasNextLine() with nextLine() to process multi-line input one line at a time.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner("line one\nline two\nline three");
        while (scanner.hasNextLine()) {
            System.out.println("> " + scanner.nextLine());
        }
        scanner.close();
    }
}

A Real Keyboard Example

For actual user input you would write the lines below. Try them locally: the program waits at nextLine until you type and press Enter.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = scanner.nextLine();
        System.out.println("Hello, " + name);
        scanner.close();
    }
}

Closing the Scanner

Calling close() releases the resource. A cleaner approach is try-with-resources, which closes the scanner automatically when the block ends.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner("data here")) {
            System.out.println(scanner.next());
        }
    }
}

next Versus nextLine

Remember the key difference: next() stops at whitespace and returns one token, while nextLine() grabs the rest of the current line.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner("first second third");
        System.out.println(scanner.next());
        System.out.println(scanner.nextLine());
        scanner.close();
    }
}

Custom Delimiters

You can change what separates tokens with useDelimiter. Here a comma separates values instead of whitespace.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner("a,b,c");
        scanner.useDelimiter(",");
        while (scanner.hasNext()) {
            System.out.println(scanner.next());
        }
        scanner.close();
    }
}

Scanner Essentials

Create one Scanner on System.in, use next for words and nextLine for whole lines, loop with the hasNext family, and always close it.

Quick Check

Test your Scanner basics.

Recap

You learned to read input with Scanner:

  • Create it with new Scanner(System.in)
  • next() reads a word, nextLine() reads a whole line
  • Loop with hasNext() or hasNextLine()
  • Close it with close() or try-with-resources
  • useDelimiter changes the token separator

Frequently asked questions

Is the “Reading Input with Scanner” lesson free?

Yes — the full text of “Reading Input with Scanner” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reading Input with Scanner”?

Read user input from System.in. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Reading Input with Scanner” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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