Sub topic 8
Console I/O with `java.lang.IO`
This chapter assumes you know basic Java syntax, methods, variables, control flow, and the traditional public static void main(String[] args) method. The examples use Java 25, where compact source files and instance main methods are final features.
Console input and output let a program communicate through the terminal. Output sends text to the console. Input reads text typed by the user. Java’s java.lang.IO class provides short methods for these tasks, so a small program does not need System.out or Scanner boilerplate.
Writing console output
IO.println prints a value followed by a line break. IO.print prints a value without moving to a new line.
void main() {
IO.print("Name: ");
IO.println("Ada");
IO.println(42);
}The output is:
Name: Ada
42IO is a class in the java.lang package. Java automatically makes java.lang types available by simple name, so this compact source file can use IO directly. You could also write java.lang.IO.println("Hello");, but the shorter form is clearer.
The methods accept values such as strings, numbers, and objects. Java converts each value to text before printing it.
Reading a line
IO.readln() reads one complete line from standard input. Standard input is normally the terminal. The method returns the line as a String.
void main() {
String name = IO.readln();
IO.println("Hello, " + name + "!");
}Run the file and type a name:
Ada
Hello, Ada!You can pass a prompt to readln. The prompt is printed without a line break before Java waits for input.
void main() {
String name = IO.readln("Your name: ");
IO.println("Welcome, " + name + "!");
}A line read from the console is always text. Convert it when you need a number.
void main() {
String input = IO.readln("How many apples? ");
int apples = Integer.parseInt(input);
IO.println("You have " + apples + " apples.");
}Integer.parseInt converts a string such as "12" to the integer 12. If the user enters something that is not a valid integer, it throws NumberFormatException.
The input and output flow
A small interactive program usually follows this sequence:
If the input stream reaches its end before another line is available, IO.readln() can return null. Code that reads from redirected input should account for that possibility. For a person typing at an open terminal, the usual case is a non-null string, including an empty string when the user presses Enter without typing anything.
Common errors
- Expecting
readlnto return anint. It returns aString. - Forgetting that an empty line is different from a missing line.
- Using
IO.printwhen a line break is required. - Assuming invalid numeric input is silently accepted. Parsing methods report invalid text with an exception.
- Compiling with an older Java release that does not provide the Java 25
IOAPI.
For simple exercises, IO is convenient and readable. Larger applications may use other input libraries when they need validation frameworks, files, network streams, or more control over character encoding.
Exercise
Write a compact source file that asks for a person's age, converts the answer to an integer, and prints the age next year.
Show answer
Use String text = IO.readln("Age: ");, convert it with Integer.parseInt(text), and print age + 1.