About Us Contact Us Write for Us Advertise
Home > Java > Input and Output in Java: Reading User Input with Scanner
Java

Input and Output in Java: Reading User Input with Scanner

Learn how to make your Java programs interactive — read user input with Scanner step by step, print output cleanly, and avoid the classic nextInt()/nextLine() trap.

Shiv Pandey
Shiv Pandey
Sep 18, 2026 | 61 views
Input and Output in Java: Reading User Input with Scanner

So far, every program we've written already knew its data — the values were typed right into the code. But real programs are interactive: they ask the user for input and respond to it. In this lesson you'll learn how to read what a user types, using Java's Scanner, and how to print output cleanly. This is the moment your programs start to feel alive.

Output: printing to the screen

You've already met System.out.println(). There are two versions worth knowing:

System.out.println("Hello");   // prints, then moves to a NEW line
System.out.print("Hello");     // prints, stays on the SAME line

You can join values with + to build a message:

int score = 90;
System.out.println("Your score is " + score);   // Your score is 90

Input: reading what the user types

To read input, Java gives us a tool called Scanner. Using it is a simple three-step recipe.

Step 1: Import Scanner

At the very top of your file (above the class), add this line. It tells Java where to find the Scanner tool:

import java.util.Scanner;

Step 2: Create a Scanner

Inside your main method, create a Scanner that reads from the keyboard (System.in):

Scanner sc = new Scanner(System.in);

Step 3: Read a value

Now ask the Scanner for input. The method you call depends on the type of value you want:

You want Method
A whole number (int) sc.nextInt()
A decimal (double) sc.nextDouble()
A single word sc.next()
A whole line of text sc.nextLine()

A complete example

Let's put it all together — a program that asks for your name and age, then greets you:

import java.util.Scanner;

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

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

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

        System.out.println("Hello, " + name + "! Next year you'll be " + (age + 1) + ".");
    }
}

Run it, and the program pauses at each print waiting for you to type and press Enter. Notice we prompt first (print(...)), then read — always tell the user what you want before asking for it.

The one gotcha everyone hits: nextInt() then nextLine()

This trips up every beginner, so let me save you the headache. If you call nextInt() (or nextDouble()) and then nextLine(), the second one seems to get "skipped."

Why? nextInt() reads the number but leaves the invisible "Enter" (newline) sitting in the buffer. The next nextLine() instantly reads that leftover newline and returns an empty string.

The fix — add an extra sc.nextLine() right after the number to "eat" that leftover newline:

int age = sc.nextInt();
sc.nextLine();            // consume the leftover newline
String city = sc.nextLine();   // now this works correctly

Remember this one — it will save you a very confusing half hour someday.

Formatted output with printf

When you want neat, controlled output (like money with two decimals), printf is handy:

double price = 9.5;
System.out.printf("Total: %.2f%n", price);   // Total: 9.50

Here %.2f means "a decimal number with 2 digits after the point," and %n is a new line. Don't worry about memorising this now — just know it exists for when you need tidy output.

The nextInt()/nextLine() gotcha genuinely stumped me for an evening when I was learning — I was convinced my program was broken. Once someone explained the leftover-newline reason, it clicked forever. Now you get to skip that evening entirely.

Key takeaways

  • System.out.print() stays on the same line; println() moves to a new line.
  • Read input in 3 steps: import Scanner, create new Scanner(System.in), then read with nextInt(), nextDouble(), next(), or nextLine().
  • Always prompt before you read so the user knows what to type.
  • After nextInt(), add an extra nextLine() before reading a full line of text.

← Previous: Lesson 5 — Operators in Java
Next: Lesson 7 — Decisions: if, else & switch →

Related Articles

File I/O in Java: Reading and Writing Files the Modern Way
Java

File I/O in Java: Reading and Writing Files the Modern Way

== vs .equals() in Java: The Difference Explained (Beginner Guide)
Java

== vs .equals() in Java: The Difference Explained (Beginner Guide)

Java vs Python: Which Should You Learn? (Beginner Guide)
Java

Java vs Python: Which Should You Learn? (Beginner Guide)

Build a Java Console Project: A To-Do App (Step by Step)
Java

Build a Java Console Project: A To-Do App (Step by Step)