java.util.NoSuchElementException: No line found

前端 未结 6 810
失恋的感觉
失恋的感觉 2020-11-22 10:17

I got an run time exception in my program while I am reading a file through a Scanner.

java.util.NoSuchElementException: No line found     
   at java.util         


        
6条回答
  •  没有蜡笔的小新
    2020-11-22 10:38

    Your real problem is that you are calling "sc.nextLine()" MORE TIMES than the number of lines.

    For example, if you have only TEN input lines, then you can ONLY call "sc.nextLine()" TEN times.

    Every time you call "sc.nextLine()", one input line will be consumed. If you call "sc.nextLine()" MORE TIMES than the number of lines, you will have an exception called

          "java.util.NoSuchElementException: No line found".
    

    If you have to call "sc.nextLine()" n times, then you have to have at least n lines.

    Try to change your code to match the number of times you call "sc.nextLine()" with the number of lines, and I guarantee that your problem will be solved.

提交回复
热议问题