Sub topic 9
Reading with `java.io.IO` (Java 25)
Prerequisites and purpose
This chapter assumes that you know basic Java syntax, variables, methods, String, primitive types, and how to run a Java program. It also assumes that you have used Scanner for console input.
Java 25 provides java.io.IO, a small input and output helper designed for compact source files. Its readln method displays an optional prompt and reads one complete line. Unlike Scanner.nextInt(), it always starts with text. You convert that text to a number yourself.
Reading a line
A Java 25 compact source file can contain a main method without a surrounding class:
void main() {
String name = IO.readln("Name: ");
int age = Integer.parseInt(IO.readln("Age: ").trim());
IO.println(name + " is " + age);
}Save this as Greeter.java and run it with:
java Greeter.javaA possible session is:
Name: Ada
Age: 36
Ada is 36IO.readln("Name: ") prints the prompt and returns the line typed by the user. The returned value has type String.
trim() removes whitespace at the beginning and end of the line. Integer.parseInt converts the cleaned string to an int. If the user types text such as thirty-six, parsing fails with a NumberFormatException.
In a regular class, use an import:
import java.io.IO;
public class Greeter {
public static void main(String[] args) {
String name = IO.readln("Name: ");
int age = Integer.parseInt(IO.readln("Age: ").trim());
IO.println(name + " is " + age);
}
}The compact form reduces ceremony for small programs. The regular class form fits applications that already have classes, packages, tests, and multiple methods.
How `IO` differs from `Scanner`
Scanner can tokenize input and convert tokens directly:
int age = in.nextInt();IO.readln reads a whole line, so conversion is explicit:
int age = Integer.parseInt(IO.readln("Age: ").trim());Line-based input often makes prompts easier to reason about. The complete response is captured first, and your code decides how to validate or convert it.
| Need | Suitable approach |
|---|---|
| Read a few complete responses in a small Java 25 program | `IO.readln` |
| Read mixed tokens and numbers with built-in token conversion | `Scanner` |
| Read lines in older Java versions with explicit exception handling | `BufferedReader` |
Validate converted input
IO.readln does not validate numeric text for you. A loop can catch invalid values and ask again:
void main() {
while (true) {
String text = IO.readln("Enter an integer: ");
try {
int number = Integer.parseInt(text.trim());
IO.println("You entered " + number);
break;
} catch (NumberFormatException ex) {
IO.println("Please enter a whole number.");
}
}
}This approach validates the entire line. For example, 12abc is rejected instead of being partly accepted. You can use Double.parseDouble when the expected value is a double.
Input flow
Choosing `IO`
Use java.io.IO when all of these conditions fit:
- The program targets Java 25 or later.
- Input is naturally entered one line at a time.
- A compact source file or a small helper API improves readability.
- You are prepared to convert and validate returned strings.
For code that must run on earlier Java releases, use Scanner or BufferedReader instead. For passwords, use Console.readPassword, because it can prevent typed characters from appearing on screen.
Exercise
Why does the example use Integer.parseInt(IO.readln("Age: ").trim()) instead of assigning IO.readln directly to an int?
Show answer
IO.readln returns a String, while an int requires numeric conversion. trim removes surrounding whitespace, and Integer.parseInt performs the conversion.