How to use multiple Scanner objects on System.in?

百般思念 提交于 2019-11-26 15:31:26

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 an InputStream

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

See also Java code question ... scanner related? which also talks about some approaches.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!