Classroom Grade Analyzer: bad initializer for for-loop?


#1

Hey guys,

so I've been working on the class grade analyzer. I think I did all the steps correctly yet I still get an error saying:

GradeAnalyzer.java:17: error: bad initializer for for-loop
for(Integer : grades){

The code up until the line in question looks like this:

> import java.util.ArrayList;

> public class GradeAnalyzer{

>   //Constructor
>   public GradeAnalyzer(){

>   }

>   public int getAverage(ArrayList<Integer> grades){
>   	if (grades.size < 1){
>     	System.out.println("The ArrayList is empty!");       
> return 0;
>     }
>     else {
>     	int sum = 0;
>       **for(Integer : grades){**
>       	sum = sum + Integer;
>       }
>     }
>     int average = sum / grades.size;
>     System.out.println(average);
>     return average;
>  	}

I'm struggling to understand what I did wrong here. Can somebody explain this?

Thank you very much!


#2

The thing is you want to use a for/each loop iterate an array that stores integers remember Java is a strongly typed language meaning ever declaration must have a type.
Now you need an iterator that is of type integer too but then you have to give it a name so when you write that line above it is an error. You need to write it like this

   for(Integer s : grades){
  	 sum = sum + s;
   }