Simple Console I/O with the IO Class

Naveen AnamaneniUpdated · 4 min read

To ask a user for their name with the tools from the last two lessons, you need an import, a Scanner object and System.out.print. That's three new ideas before your program has done anything. Java 25 added a small class called IO that cuts this down to one line: String name = IO.readln("Your name: ");. It's built for learning, quick tools and small scripts.

Three Methods, Nothing to Import

IO lives in java.lang, the package every Java program gets automatically, so there's no import. All its methods are static:

IO methodDoes the same asReturns
IO.println(x)System.out.println(x)nothing
IO.println()System.out.println()nothing
IO.print(x)System.out.print(x)nothing
IO.readln()Read one line from the keyboardString
IO.readln(prompt)print(prompt), then read one lineString

A Fare Calculator

FareCheck.java▶ Try it
void main() {
    String name = IO.readln("Passenger name: ");
    String countText = IO.readln("How many tickets? ");
    int count = Integer.parseInt(countText.trim());
    int total = count * 120;
    IO.println(name + " pays Rs. " + total);
}
Output
Passenger name: How many tickets? Navika pays Rs. 480

On your screen, with your typing, it looks like this:

Terminal
Passenger name: Navika
How many tickets? 4
Navika pays Rs. 480

How it works

  1. Line 2 prints the prompt and waits. Whatever you type, up to Enter, comes back as a String.
  2. Line 3 does the same for the number of tickets, still as text.
  3. Line 4 converts the text to an int. .trim() removes stray spaces, which would make parseInt fail.
  4. Line 6 prints the result.

This file is a compact source file (no class around main), covered in "Compact Source Files and Instance Main Methods". IO works just as well inside a normal class.

Try it yourself: type four instead of 4. Which exception appears, and on which line?

No nextInt Trap

Because readln always reads a whole line, there's no leftover Enter to trip over, unlike Scanner.nextInt() followed by nextLine(). The price is that every number arrives as text and needs Integer.parseInt or Double.parseDouble.

When Input Runs Out

If there's no more input, for example when input comes from a file that has ended, readln returns null. Calling a method on null, such as .trim(), then throws a NullPointerException. Interactive programs rarely hit this, but scripts that read piped input should check:

Lines.java
String line = IO.readln();
if (line == null) {
    IO.println("No input");
}

Common Mistakes

Running on an older JDK. IO exists only from Java 25. On Java 21 you get cannot find symbol: variable IO.

Expecting readln to return a number. It returns String. int n = IO.readln(); doesn't compile.

Using IO.printf. There's no printf in IO. Use IO.println(String.format(...)) or System.out.printf.

Practice

  1. Easy: Ask for the user's city with IO.readln and print Welcome to <city>!.
  2. Harder: Ask for a price and a GST percentage, both with IO.readln, and print the total with two decimals using String.format("%.2f", ...).

Quick check: The IO class

4 questions
1Which import do you need for IO?
2What does IO.readln("Age: ") return?
3What does readln return when there's no more input?
4From which Java version is IO available?

Key Takeaways

Was this tutorial helpful?

Lesson 51 of 482