How to use multiple Scanner objects on System.in in Java?

What am I doing wrong?

Using multiple scanners on the same stream is the underlying problem. Scanners can (and will) consume the stream – this may (will) lead to unexpected side-effects. Best not to do it.

If the input is closed, then the input (but Strings have no close method) is closed for everyone – and that’s not much fun for anyone.

Edit:
“Details” on why multiple scanners are bad:
Do not create multiple buffered wrappers on a single byte or character stream

…any buffered wrapper is unsafe; this condition is also exploitable if a Scanner is used instead…

Leave a Comment