GradeAnalyzer error, pls help


#1

GradeAnalyzer.java:18: error: cannot find symbol
int average = sum / grades.size();
^
symbol: variable sum
location: class GradeAnalyzer
1 error

import java.util.ArrayList;

public class GradeAnalyzer {
  
  public GradeAnalyzer() {  
  }
  int getAverage(ArrayList<Integer> grades) {
    
    if (grades.size() < 1) {
      System.out.println("Error! ArrayList is empty.");
      return 0;
    } else {
      int sum = 0;
      for (Integer grade : grades){
        sum = sum + grades.size();
      }
    }
    int average = sum / grades.size();
    System.out.println(average);
    return average;
  }
  public static void main(String[] args) {
    ArrayList<Integer> myClassroom = new
      ArrayList<Integer>();
    myClassroom.add(98);
    myClassroom.add(92);
    myClassroom.add(88);
    myClassroom.add(75);
    myClassroom.add(61);
    myClassroom.add(89);
    myClassroom.add(95);
    
    GradeAnalyzer myAnalyzer = new GradeAnalyzer();
    myAnalyzer.getAverage(myClassroom);
  }
}

#2

There's no such variable in that scope. You are creating a variable by that name somewhere else, but execution has left that scope by the time it reaches line 18

You'll need to decide whether the variable should be created somewhere else or if you shouldn't be using it at all in that location


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.