我有以下代码,我无法理解为什么.hasNext()
不会变错。我正在读一个名为test的文件。
我的代码:
package printing;
import java.io.File;
import java.util.Scanner;
public class Printer {
public int count() throws Exception{
int num = 0;
File f = new File("C:\\Users\\bob\\Desktop\\test.txt");
Scanner in = new Scanner(f);
while (in.hasNext()){
num++;
}
return num;
}
}
主要:
public class Main {
public static void main(String[] args) throws Exception{
Printer mine = new Printer();
System.out.println(mine.count());
}
}
文件内容:
4 4 6 3 8 8 8
有什么问题?