Type safety of input.nextLine?

瘦欲@ 提交于 2019-12-12 16:10:07

问题


Would there ever be a case where an exception could be thrown by assigning the value of input.nextLine() to a String variable with the Scanner?

Like if you put

String foo = input.nextInt();

You would get an InputMismatchException. So what I'm wondering is if there's any possible way to get an exception from:

String foo = input.nextLine();

It might be a dumb question but I need to be absolutely sure.

Thanks in advance!


回答1:


The answer in the docs:

Throws:
NoSuchElementException - if no line was found
IllegalStateException - if this scanner is closed

Tip for easier and safer life: Always refer to the docs.



来源:https://stackoverflow.com/questions/20331645/type-safety-of-input-nextline

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