So I am trying to make a code that will read a file that contains an unknown number of students and an unknown amount of scores for each student. Then output each students average score. I have tried several different combinations of loops and each times some problem comes up. Unreachable statement, unrecogniable variable, etc.

import java.util.Scanner;
import java.io.File;
import java.text.DecimalFormat;

// Opens a file of test scores and calls processScores to read and evaluate them.
public class TestResults {
    public final static String filename = "testScores.txt";

    public static void main(String[] args) {
        Scanner inputFile = null;
        try {
            inputFile = new Scanner(new File(filename));
        } catch (Exception e) {
            System.out.println("File could not be opened: " + filename);
            System.exit(0);
        }

        processScores(inputFile);
    }

    // Method that looks at the test scores of an unknown amount of
    //students and outputs each students average and if they passed or not
    public static void processScores(Scanner file) {
        DecimalFormat df = new DecimalFormat("00.0");   // For outputing test average
        System.out.println("Name           Avg  Pass");
        System.out.println("------------------------");

        String name;
        double score = 0;
        double average = 0;

        while (file.hasNext());{

            for(int n = 0; ; n++){
                name = file.next();
                System.out.print(name);

                //scores
                for(double s = 0; ; s++){
                    score = file.nextDouble();

                }
                average = average + score;
                System.out.print(average);
                System.out.println();
                average = 0;

            }
        }
    }         

} 

Recommended Answers

All 7 Replies

I think you would be better of learning a bit more about IO operations in Java.

Can you explain what you are trying to do in the following lines.

 //scores
for(double s = 0; ; s++){
score = file.nextDouble();
}
average = average + score;
System.out.print(average);

In the end of the loop, the variable score will have only the last entry from the file.
And the variable average will also have the same value only.

When reading the file how do you know how many scores there are for each student?

What does the input file look like? Let's see the data.

ps Line 32
while (file.hasNext());{

Note the effect of the semicolon, which means it's really like this

  while (file.hasNext()); // loop until end of file, doing nothing.
    {  // start of a new block, nothing to do with the loop.  

loop until end of file, doing nothing.

hasNext doesn't mutate or change the cursor position. So the while loop results in an infinite loop since hasNext will always return true (for a non-empty file) given no lines are consumed. Basically, the code will only proceed if the file is completely empty.

Yes, that's right, sorry I missed that point because I was focussed on the semicolon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.