At a loss on what's wrong with my code. Please help an idiot

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/learn-java/projects/grades-analyzer

<In what way does your code behave incorrectly? Include ALL error messages.>
GradeAnalyzer.java:16: error: cannot find symbol
int average = sum / grades.size();
^
symbol: variable sum
location: class GradeAnalyzer
1 error

```

import java.util.ArrayList;

class GradeAnalyzer{
public GradeAnalyzer(){
}
public int getAverage(ArrayList grades){
if (grades.size() < 1){
System.out.println(“Error! ArrayList is empty.”);
return 0;
}
else{
int sum = 0;
for (Integer grade : grades){
sum = sum + grade;
}
}
int average = sum / grades.size();
System.out.println(average);
return average;
}

public static void main(String args){
ArrayList myClassroom = new ArrayList();
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);

}
}

<do not remove the three backticks above>

Sum is defined in this scope

So it will only exist in the scope not outside of it. You are trying to use that variable outside of its scope here

that’s why the compiler is complaining that it does not exist

Thank you for you help!

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