Simple Console I/O with the IO Class
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 method | Does the same as | Returns |
|---|---|---|
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 keyboard | String |
IO.readln(prompt) | print(prompt), then read one line | String |
A Fare Calculator
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);
}Passenger name: How many tickets? Navika pays Rs. 480On your screen, with your typing, it looks like this:
Passenger name: Navika
How many tickets? 4
Navika pays Rs. 480How it works
- Line 2 prints the prompt and waits. Whatever you type, up to Enter, comes back as a
String. - Line 3 does the same for the number of tickets, still as text.
- Line 4 converts the text to an
int..trim()removes stray spaces, which would makeparseIntfail. - 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:
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
- Easy: Ask for the user's city with
IO.readlnand printWelcome to <city>!. - Harder: Ask for a price and a GST percentage, both with
IO.readln, and print the total with two decimals usingString.format("%.2f", ...).
Key Takeaways
Was this tutorial helpful?