User Input

Interactive programs read input from the user. In console programs this is usually done with the Scanner class, which reads text and converts it into numbers and other types. Java 25 also adds a simpler IO.readln. This lesson covers both, and how to handle input that isn't what you expected.

Reading a line with Scanner

JAVA
import java.util.Scanner;

void main() {
    Scanner in = new Scanner(System.in);
    System.out.print("What's your name? ");
    String name = in.nextLine();
    System.out.println("Hello, " + name + "!");
}
Input typed by the user
Ana
Output
What's your name? Hello, Ana!

new Scanner(System.in) reads from the keyboard (standard input). nextLine() waits for the user to press Enter and returns the whole line. (In the output above, the typed "Ana" doesn't appear because it was piped in rather than typed at a terminal.)

Reading numbers

JAVA
import java.util.Scanner;

void main() {
    Scanner in = new Scanner(System.in);
    System.out.print("Width: ");
    double width = in.nextDouble();
    System.out.print("Height: ");
    double height = in.nextDouble();
    System.out.println("Area: " + width * height);
}
Input
4.5
2
Output
Width: Height: Area: 9.0
MethodReads
nextLine()The rest of the current line
next()The next word (up to whitespace)
nextInt(), nextLong()A whole number
nextDouble()A decimal number
nextBoolean()true or false
hasNextInt(), hasNextLine()…Checks whether such input is available

The nextInt / nextLine trap

JAVA
import java.util.Scanner;

void main() {
    Scanner in = new Scanner(System.in);
    System.out.print("Age: ");
    int age = in.nextInt();
    System.out.print("City: ");
    String city = in.nextLine();
    System.out.println("[" + age + "][" + city + "]");
}
Input
30
Lisbon
Output
Age: City: [30][]

nextInt() reads the number but leaves the Enter key's line break behind; the following nextLine() immediately reads that empty remainder. Fix it by calling in.nextLine() once after nextInt(), or — simpler and more robust — read every value as a line and parse it:

JAVA
import java.util.Scanner;

void main() {
    Scanner in = new Scanner(System.in);
    System.out.print("Age: ");
    int age = Integer.parseInt(in.nextLine().strip());
    System.out.print("City: ");
    String city = in.nextLine();
    System.out.println("[" + age + "][" + city + "]");
}
Input
30
Lisbon
Output
Age: City: [30][Lisbon]

Validating input

JAVA
import java.util.Scanner;

void main() {
    Scanner in = new Scanner(System.in);
    int quantity;
    while (true) {
        System.out.print("Quantity (1-10): ");
        String line = in.nextLine();
        try {
            quantity = Integer.parseInt(line);
            if (quantity >= 1 && quantity <= 10) break;
        } catch (NumberFormatException e) {
            // not a number: fall through and ask again
        }
        System.out.println("Please enter a whole number from 1 to 10.");
    }
    System.out.println("You ordered " + quantity);
}
Input
lots
42
3
Output
Quantity (1-10): Please enter a whole number from 1 to 10.
Quantity (1-10): Please enter a whole number from 1 to 10.
Quantity (1-10): You ordered 3

Never trust input: users mistype, and files contain surprises. Loops (lessons 15–17) and exceptions (lesson 36) let you ask again instead of crashing.

IO.readln (Java 25)

JAVA
void main() {
    String name = IO.readln("Your name: ");
    int year = Integer.parseInt(IO.readln("Birth year: "));
    IO.println(name + " turns " + (2026 - year) + " this year.");
}
Input
Mei
1990
Output
Your name: Birth year: Mei turns 36 this year.

The new IO class prints a prompt and reads a line in one call — convenient for small programs and learning. Scanner remains the general tool, and works on every Java version.

Command-line arguments

JAVA Greet.java
public class Greet {
    public static void main(String[] args) {
        System.out.println("Arguments: " + args.length);
        for (String arg : args) {
            System.out.println("Hello, " + arg);
        }
    }
}
Output
Arguments: 0

Values typed after the program name — java Greet.java Ana Ben — arrive in the args array, printing "Hello, Ana" and "Hello, Ben". Arguments are common for tools and scripts.

Try it yourself

Write a tip calculator that asks for the bill and a tip percentage. Run it locally and type the values when asked.

Try it yourself · Main.java
Edit the code, copy it into Main.java and run java Main.java (Java 25+). How to set up Java
Input
48.60
15
Output
Bill amount: Tip %: Total to pay: 55.89