Scanner on text file hasNext() is infinite

后端 未结 4 1569
别跟我提以往
别跟我提以往 2020-12-03 16:17

I\'m writing a simple program in Java and it requires reading data from a text file. However, I\'m having trouble counting lines. The issue seems generic enough for a simple

4条回答
  •  醉话见心
    2020-12-03 16:34

    inFile.hasNext() does not move the pointer to the next line

    try this

    String x=null;
    while((x = inFile.next()) != null)
         count++;
    

    description of hasNext()

    Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.

    http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#hasNext%28%29

提交回复
热议问题