I'm supposed to be taking all the averages of the students grades and create an overall average...but it's not working

import java.io.*;
import java.util.*;

public class Grade
    public static void main(String[] args) throws IOException

        String []name = new String[50];

        int num1, num2, num3;

        Scanner inFile = 
            new Scanner(new FileReader("Average.txt"));                 

        int []average = new int[50];

        double sum;

        int c = 0;

            name[c] = inFile.next();
            num1 = inFile.nextInt();
            num2 = inFile.nextInt();
            num3 = inFile.nextInt();            

            average[c]=((num1 + num2 + num3)/3);


        for (int i = 0; i < c; i++)
        {   System.out.println(name[i] + " " + average[i]);

        for (int i = 0; i < average.length; i++)
        sum += average[i] * (c));


it's the sum that isn't working...any ideas?...sorry to be so vague

Recommended Answers

All 7 Replies

For one, you have an extra right parenthesis after the "c". Why are you multiplying by each average by c though?

Um C is how many people are in the class...like there is the average of each person and then the class average

actually I'm not complete sure what I'm doing with that but it makes sense I think

basically all of this

for (int i = 0; i < average.length; i++)
sum += average * (c));

could be changed

I think what you probably need to do is sum all of the students' scores and then divide by the number of students. The average should be either a float or a double. Since all the scores are presumably int's and the number of students is an int. In Java an int/int calculation equals an int, so you will need to perform a cast.

double average = (double) sum / numStudents;

Yes, average[] could be made a double unless you really need it to be the int value. A cast on the final calc isn't needed though because "sum" is already a double and the double/int result will still be a double.

I need all the averages I collected in the array to be added together and divided by the number of students (c) to equal the sum. I just don't know how to write it out.

The problem isn't the logic it's just how to do it...whenever I type in something like sum += average / c it says sum may not be initialized...I don't know how to put it.

Initialize sum to 0 when you declare it. The average is just

for (int i = 0; i < average.length; i++)
  sum += average[i];
double overallAvg = sum / c;
Be a part of the DaniWeb community

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