为什么这段代码会触发无限循环?

alg*_*der 0 java infinite-loop

我正在编写一些简单的代码来解析文件并返回行数,但是eclipse中的小红框不会消失所以我假设我触发了一个无限循环.我正在阅读的文本文件只有10行......这是代码:我做错了什么?

import java.io.*;
import java.util.Scanner;
public class TestParse {    
    private int noLines = 0;    
    public static void main (String[]args) throws IOException {
        Scanner defaultFR = new Scanner (new FileReader ("C:\\workspace\\Recommender\\src\\IMDBTop10.txt"));
        TestParse demo = new TestParse();
        demo.nLines (defaultFR);
        int x = demo.getNoLines ();
        System.out.println (x);
    }   
    public TestParse() throws IOException
    {
        noLines = 0;
    }
    public void nLines (Scanner s) {
        try {
            while (s.hasNextLine ())
                noLines++;
        }
        finally {
                if (s!=null) s.close ();
        }
    }
    public int getNoLines () {
        return noLines;
    }           
}
Run Code Online (Sandbox Code Playgroud)

typ*_*.pl 6

你没有s.nextLine()在while循环中调用:

应该:

        while(s.hasNextLine()){
           s.nextLine(); // <<<
            noLines++;

          }
Run Code Online (Sandbox Code Playgroud)