Scanner close after use

烂漫一生 提交于 2019-11-28 02:21:30

The problem is

When a Scanner is closed, it will close its input source if the source implements the Closeable interface.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

Thus scan.close() closes System.in.

To fix it you can make

Scanner scan static or pass every time scanner object in method and close it at last.

I think because you are not deleting the scanner. Try to delete the instance of s before calling it again. And if memory isn't a constraint you could always make a new scanner object, but it's better coding practice to properly de-allocate memory when you are done.

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