Reputation: 1
When i run this, the reader only reads and displays every other line. What am i doing wrong?
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);
Upvotes: 0
Views: 65
Reputation: 124265
You are using imputFile.nextDouble()
in your loop twice:
grade = imputFile.nextDouble();
//....
sum += imputFile.nextDouble();
Try changing last line to
sum += grade;
to use already read value, not to read next one.
Upvotes: 2