What is wrong with my code? Thanks

<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/courses/learn-java/projects/grades-analyzer?user_id=5302f848282ae3e91d001ead

<In what way does your code behave incorrectly? Include ALL error messages.>
GradeAnalyzer.java:30: error: class, interface, or enum expected
public static void main(String args) {
^
GradeAnalyzer.java:33: error: class, interface, or enum expected
myClassroom.add(98);

```

class GradeAnalyzer {

public GradeAnalyzer(){

}

public int getAverage(ArrayListgrades) {

if(grades<1){
System.out.println(“Sorry, this ArrayList is empty!”);
return(0);
}

else {
int sum = 0;
for (Integer grade : grades){
int sum = sum + grade;
}
}

int average = sum / grades.size();
System.out.println(“The average is” + 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>

There are really many problems in your code:

  • main method should be inside the GradeAnalyzer class;
  • to use ArrayList you have to import it;
  • here - grades<1, you are comparing ArrayList<Integer> with a plain number, does not make much sense, use size method to get the length;
  • int sum should be defined in the upper scope;
  • in the for loop you don’t want to redefine the variable, you only want to change its value, no need for int.

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