java.util.NoSuchElementException: No line found

前端 未结 6 863
失恋的感觉
失恋的感觉 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:45

    Need to use top comment but also pay attention to nextLine(). To eliminate this error only call

    sc.nextLine()
    

    Once from inside your while loop

     while (sc.hasNextLine()) {sc.nextLine()...}
    

    You are using while to look ahead only 1 line. Then using sc.nextLine() to read 2 lines ahead of the single line you asked the while loop to look ahead.

    Also change the multiple IF statements to IF, ELSE to avoid reading more than one line also.

提交回复
热议问题