【发布时间】:2014-06-04 01:04:48
【问题描述】:
我有一个文件,每行都有一个不同的整数。例如:
5
4
3
2
1
我正在尝试编写一个程序来运行每个 int,并将该 int 放入一个数组中。到目前为止,我的代码是:
Scanner sc = new Scanner(args[0]);
BufferedReader reader = new BufferedReader(new FileReader(args[0]));
lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
intArray = new int[lines];
int counter = 0;
while(sc.hasNextInt()) {
intArray[counter] =sc.nextInt();
counter++;
}
我的程序创建了具有正确数量索引的数组,但它永远不会进入扫描仪的 while 循环。我不知道这是为什么,因为根据page 看起来我有相同的代码。
【问题讨论】:
标签: java arrays java.util.scanner