How to get average of test scores organized into columns in a file?

The file contains the data in the following format:

John Smith|82|89|90|78|89|96|75|88|90|96
Jane Doe|90|92|93|90|89|84|97|91|87|91
Joseph Cruz|68|74|78|81|79|86|80|81|82|87
Suzanne Nguyen|79|83|85|89|81|79|86|92|87|88

I am trying to find out how to get the sum of each column (Ex. Test 1 = 82 + 92 + 68 + …) to ultimately calculate the average score for each test.

This is how I parsed the file and did the other calculations:

public class TestAverages
  private static int[] grades;
  private static int[] testTotal;
  private static int N;
  private static double classTotal;
  public static void main(String[] args) throws FileNotFoundException
    File input = new File("TestData.txt");
    Scanner in = new Scanner(input);
  public static void parseFile(Scanner in) throws FileNotFoundException
    TestAverages t = new TestAverages();
    in.nextLine(); //skips the first line of text in file (labels)
    double classAvg =0.0;
      String line = in.nextLine();
      String[] data = line.split("\|");
      String name = data[0];
      grades = new int[data.length - 1];
      N = grades.length;
      for(int i = 0; i < N; i++)
        grades[i] = Integer.parseInt(data[i + 1]);

      System.out.println("Student Average: " + t.getStudentAvg(grades) + "%n");
      classAvg = t.getClassAvg(grades);
      System.out.println("Test Average: " + t.getTestAvg(grades) + "%n");
    System.out.printf("nClass Average: %.2f%%n", classAvg );
  public double getStudentAvg(int[] grades)
    double total = 0.0;
    double avg = 0.0;
    int N = grades.length;
    for(int i = 0; i < N; i++){
      total += grades[i];}
    avg = total / N;
    return avg;
  public double getClassAvg(int[] grades)
    double classTotal = getStudentAvg(grades) / N;
    double classAvg =0.0;
    classTotal += classTotal;
    classAvg = classTotal;
    return classTotal;

Please excuse my formatting if it’s not up to par to the standard.

My main issue at the moment is how to extract the score for each test for each student and add everything up.


The test scores for each student are being calculated. What you need are the class average and the test average.

  • The test averages are the average scores on each test.
  • The student averages are the average test scores for each student
  • The class averages is how the overall class did on all the tests.

Consider three tests

            test1   test2  test3   student average
student1      90      80     70          240/3
student2     100      90     90          280/3
student3      80      80     90          250/3

testAvg      270/3    250/3  250/3

class avg = (270 + 250 + 250)/9   or     (240+280+250)/9      

So you need to read in the values in such a way so as to facilitate making the other calculations. I would recommend either a Map<String,List<Integer>> for the data where the string is the students name and the list is the test scores for each student. Or you could use a 2D int array. But you need to save the scores so you can do the column average for the test scores. Most of the work is done.

Also, two problems I noticed. You call the method getTestAvg but don’t declare it and you declare the method getClassAvg but never call it.

Leave a Reply

Your email address will not be published. Required fields are marked *