Adding a little bit more detail to my comments
A try-with
block is defined as follows:
try(...) {
...
}
where the argument in parenthesis needs to be an instance of java.lang.AutoCloseable
. An example is the class java.io.InputStream
, which is also the class for System.in
.
A try-with
attempts to automatically close its provided resource, once the block is left. Depending on the used resource, it closes all its own child resources as well.
Taking your example, you have try(Scanner scanner = new Scanner(System.in))
, which uses Scanner
as resource. The scanner itself uses System.in
as resource. Once the try
block is left (when }
is reached) it tries to close its resources, which is the Scanner
instance. This instance also tries to close its resource, the System.in
.
Once System.in
is closed, you can't get any input from the console anymore (at least not with some additional work, I think...).
Concretely, in your second example:
while (!input.equals("q")) {
try(Scanner scanner = new Scanner(System.in)){
...
} // <--- The block is left, scanner is closed, System.in is closed
} // <-- start a new iteration
Here after just one iteration, System.in
gets closed. Sure, you create a new Scanner
in the next iteration, but System.in
remains closed, that's why you get your exception in this case.
Your third example:
try(Scanner scanner = new Scanner(System.in)){
while (!input.equals("q")) {
...
} // <-- start a new iteration, while still in the same try block
} // <-- only after the while, your resources are closed
Here you're looping your while
, while still being inside try
. So no resource gets closed, until you leave while
and try
. That means, the one Scanner
remains intact and with it the one System.in
. This allows you to keep reading from the console until you're done looping.