Final Errors

So far the following is that i have for code

import java.util.ArrayList;

 public class GradeAnalyzer {
   public GradeAnalyzer(){
     
   }
  public int getAverage(){
    ArrayList<Integer> grades = new ArrayList<Integer>();
  
    if (grades.size() < 1){
      System.out.println("Array List is Empty");
      return (0);
    }
    else {
      int sum = 0;
      for (Integer grade: grades){
        sum = grades.get(grade);
      }
      
    }
    int average = sum/grades.size();
    System.out.println("Average is " +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);
   }
 }

Could somoene tell me why im getting the following error messages/ how I can fix this?

GradeAnalyzer.java:21: error: cannot find symbol
    int average = sum/grades.size();
                  ^
  symbol:   variable sum
  location: class GradeAnalyzer
GradeAnalyzer.java:37: error: method getAverage in class GradeAnalyzer cannot be applied to given types;
     myAnalyzer.getAverage(myClassroom);
               ^
  required: no arguments
  found: ArrayList<Integer>
  reason: actual and formal argument lists differ in length
2 errors

Just editing your post so we can read it properly. Please read the article on how to format code, for the next time you post :slight_smile:

OKay so this error message tells you everything you need to know.
You a trying to call a method getAverage with the argument myClassroom but when you defined the method it was defined without any arguments.

What we want to do is pass the method getAverage an argument grades.
What you are doing in the second line is just declaring a new grades variable to an empty array.

So to pass an argument we need to put it in the ()
So get rid of the line where you declare grades and instead you should have:

public int getAverage(ArrayList<Integer> grades){

So it is passed as an argument.

The other error you have is the variable sum is out of scope. sum only has scope within the else block. Try declaring it at the start of the method to give it the scope for the whole method.

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