在读取试图确定有效等级的文件时,为什么文件阅读器会跳过行?

时间:2013-10-03 23:48:58

标签: java file filereader

当我运行它时,阅读器只读取并显示每隔一行。我究竟做错了什么?

while (imputFile.hasNext()) {
            grade = imputFile.nextDouble();
            System.out.println(grade);
            if (grade < 0 || grade > 100)
                System.out.print("Grade " + grade + " was invalid and ignored");
            else {
                numberOfGrades++;
                sum += imputFile.nextDouble();
            }
        }
    averageGrade = sum/numberOfGrades;
    System.out.println("There were "+ numberOfGrades + " valid grades.");
    System.out.printf("%3.2f",averageGrade);

1 个答案:

答案 0 :(得分:2)

您在循环中使用imputFile.nextDouble()两次:

grade = imputFile.nextDouble();
//....
sum += imputFile.nextDouble();

尝试将最后一行更改为

sum += grade;

使用已读取的值,而不是读取下一个值。