java.util.NoSuchElementException: No line found

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

    with Scanner you need to check if there is a next line with hasNextLine()

    so the loop becomes

    while(sc.hasNextLine()){
        str=sc.nextLine();
        //...
    }
    

    it's readers that return null on EOF

    ofcourse in this piece of code this is dependent on whether the input is properly formatted

提交回复
热议问题