Sub topic 8
Closing the Scanner
Assume you know Java classes, methods, variables, control flow, and basic exceptions. This chapter focuses on resource ownership: deciding when a Scanner should be closed and what happens when it reads from System.in.
What closing means
System.in is Java's standard input stream. It represents input supplied by the terminal, a redirected file, or another process. A Scanner reads and converts data from that stream.
When you close a Scanner, the scanner also closes the underlying input source. Therefore, closing a scanner created with System.in closes System.in too.
Scanner in = new Scanner(System.in);
in.close();After this call, the program cannot use System.in through another scanner or reader. A later read can fail with an IllegalStateException from the scanner or an input error from another reader.
The important relationship is:
Use try-with-resources when the scanner owns the input
A try-with-resources statement closes an object automatically when the block ends. It is useful when your program creates the input stream for a limited task and no other code needs it afterward.
import java.util.Scanner;
public class SumAll {
public static void main(String[] args) {
int sum = 0;
try (Scanner in = new Scanner(System.in)) {
while (in.hasNextInt()) {
sum += in.nextInt();
}
}
System.out.println("Sum: " + sum);
}
}The Scanner is closed after the try block, even if the loop exits because of an exception. Since this scanner wraps System.in, the standard input stream is closed at that point.
This pattern is appropriate when:
- The scanner is used by the whole program.
- No later code needs to read from standard input.
- The program has reached its final input operation.
- The input source is a file or another resource that your code owns.
Do not close a shared standard input
A method should usually not close a scanner that it did not create. Otherwise, the method may unexpectedly disable input for its caller.
import java.util.Scanner;
public class MenuProgram {
static int readChoice(Scanner in) {
System.out.print("Choose 1 or 2: ");
return in.nextInt();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int choice = readChoice(in);
System.out.println("Choice: " + choice);
System.out.print("Enter your name: ");
String name = in.next();
System.out.println("Hello, " + name);
// The program is finished with input.
in.close();
}
}readChoice uses the scanner but does not close it. The caller owns the scanner and decides when its lifetime ends. This ownership rule prevents one method from breaking another method's input.
For small command-line programs, leaving a scanner on System.in open until the process exits is often harmless. The operating system releases the process resources when the program ends. For reusable code, however, explicit ownership is clearer.
Scanner versus other readers
The same ownership issue applies to other wrappers. Closing a BufferedReader or another reader wrapping System.in also closes the underlying stream. Do not mix independently managed wrappers around the same input stream unless you understand which object owns it.
| Situation | Recommended action |
|---|---|
| One scanner reads all program input | Close it when input is finished |
| A helper receives a scanner | Use it, but do not close it |
| Several components share `System.in` | Establish one owner and close only at the end |
| Scanner wraps a file | Use try-with-resources to close it |
Exercise
Exercise
A method receives a Scanner parameter and reads one integer. Should the method close the Scanner? Why?
Show answer
No. The caller owns the scanner and may need it for later input. The method should read from it without closing it.