我已经完成了定位方法,找到具有等级的txt文件,将它们读入,然后将它们放入数组中,然后输出我们需要的指定值。由于某种原因,它并没有采用所有的整数。例如,如果我输入:
6,87,23,90
它只会读取最后三个。
87、23和90
//reads the data from the file
private static int[] readExamScores(String userFile) throws FileNotFoundException{
File inputFile = new File(userFile);
Scanner stats = new Scanner(inputFile);
try{
int scores[] = new int[stats.nextInt()];
int i = 0;
while (stats.hasNext()){
scores[i] = stats.nextInt();
i++;
}
System.out.println("\n" + "There are " + (i) + " scores. \n");
Arrays.sort(scores);
return scores;
}
finally {
stats.close();
}
}
因为您的索引已经由于初始化和同时增加而移动了1例如stats.nextInt()
我建议使用arraylist作为修复程序,将其作为扫描器'消耗'如果您在字符串行中计算令牌或读取它时返回的内容。
List<Integer> scores = new ArrayList<Integer>()
int i = 0;
while (stats.hasNext()){
scores.add(stats.nextInt());
i++;
}