Sub topic 3
Closing the Scanner
This chapter assumes you know Java classes, methods, variables, control flow, String, primitive types, and basic exceptions.
Closing the Scanner
A Scanner reads from an input source. When it wraps System.in, closing the scanner also closes System.in. This is usually fine when the program has finished reading, but it can break later code that expects to read from standard input.
import java.util.Scanner;
public class CloseInput {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println(in.nextLine());
// Do not close in if another part of the program still needs System.in.
}
}Use try-with-resources when the scanner owns the input for the rest of the program:
try (Scanner in = new Scanner(System.in)) {
while (in.hasNextLine()) {
System.out.println(in.nextLine());
}
}Reading with `java.io.IO` (Java 25)
Java 25 provides IO.readln for simple line input in compact source files. It returns a String, so convert numeric input explicitly.
import java.io.IO;
void main() {
String name = IO.readln("Name: ");
int age = Integer.parseInt(IO.readln("Age: ").trim());
IO.println(name + " is " + age);
}`java.io.Console` and Passwords
Console supports prompts and hidden password input. System.console() can return null, especially when the program runs in an IDE or with redirected input.
import java.io.Console;
import java.util.Arrays;
public class Login {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.err.println("No console available");
return;
}
String user = console.readLine("User: ");
char[] password = console.readPassword("Password: ");
System.out.println("Hello, " + user);
Arrays.fill(password, ' ');
}
}Custom Delimiters
A scanner normally separates tokens at whitespace. useDelimiter changes that rule:
Scanner in = new Scanner("red|green|blue").useDelimiter("\\|");
while (in.hasNext()) {
System.out.println(in.next());
}The delimiter is a regular expression. \\| matches a literal pipe character.
`BufferedReader` as an Alternative
BufferedReader reads lines efficiently and returns null at end of input. It requires handling IOException.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Lines {
public static void main(String[] args) throws IOException {
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
}Choosing the Right Approach
- Use
Scannerfor convenient tokens, numbers, validation, and small programs. - Use
IO.readlnfor simple line-based programs on Java 25. - Use
Consolewhen you need terminal prompts or passwords. - Use
BufferedReaderfor controlled line processing and high-volume text input.
Exercise
Why might closing a Scanner created with System.in cause a later read to fail?
Show answer
Closing the Scanner also closes System.in, so later code cannot read from that stream.