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;
}
}
}
}