Java Scanner no line found, and then Scanner closed error?

旧时模样 提交于 2019-12-02 09:26:38
Naman Gala

Scanner.close() documentation states that

If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect.

On closing scanner, you are also closing System.in input stream, so when you reopen the scanner it will not find any open input stream.

Refer : java.util.NoSuchElementException - Scanner reading user input

Better pass scanner object from outside method as argument and then close it in calling method only when you are done with it.


Just to point out, is your String object str Static? If not then you can't use it in your static method. Better you remove the static from method declaration.

You have to close the scanner when everything is done.

You have closed the scanner inout stream readIn.close(); twice.

You are closing the stream before picking line by line from the file. So you have to close it once after all the instances that use readIn is finished.

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