What is wrong with my code? Thanks


#1



https://www.codecademy.com/courses/learn-java/projects/grades-analyzer?user_id=5302f848282ae3e91d001ead


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(ArrayList<Integer>grades)  {
  
  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<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 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.

#3

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