user2844723
user2844723

Reputation: 1

When reading a file trying to determine valid grades, why does the file reader skip lines?

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

Answers (1)

Pshemo
Pshemo

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

Related Questions