Bad operand types for binary operator in class grade analyzer


#1

Hey! I running into the same error in my code. It says bad operand types for binary operator, and i dont know what that means. I've searched around stackoverflow but couldn't find a solution. Here is my code:

import java.util.ArrayList;

public class GradeAnalyzer {

public GradeAnalyzer() {
}

public int getAverage(ArrayList<Integer> grades) {
if(grades < 1) {
	System.out.println("There are no grades in the ArrayList.");
   return 0;
} else {
	int sum = 0;
  for(Integer grade : grades) {
  	sum = sum + grades;
  }
	int average = sum / grades;
   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);

}

}

Any help would be appreciated. Thanks. :smile:


#2

Hey @arcwhiz56226! Glad you love Java :smile:

So first in getAverage:

In you if code here:

You cant just say if(grads>1), remember grades is an arraylist. So you do it by using the size method, like this:

if(grades.size()<1) {
	System.out.println("There are no grades in the ArrayList.");
   return 0;
}

And remember to call myClassroom on MyAlanyzer as you did here:

It has to be an Integer Array List because the parameters are an Integer ArrayList like you did here:

Hope this helped!


#3

Hello again :smile: I fixed my if statement, but i still get these errors:

GradeAnalyzer.java:15: error: bad operand types for binary operator '+'
sum = sum + grades;
^
first type: int
second type: ArrayList

GradeAnalyzer.java:17: error: bad operand types for binary operator '/'
int average = sum / grades;
^
first type: int
second type: ArrayList

I cant for the life of me figure out what i have to do ._.


#4

Hey like i said in your Adding part here:

Instead of

sum = sum + grades;

do:

sum = sum+grades.size();

and Instead of

int average = sum / grades;

do:

int average = sum / grades.size();

Hope this helped!


#5

That worked! Thanks alot :smile:


#6

@amanuel2

writing that line is correct and will allow the program to run but will yield the wrong result for average because you are adding the size of the ArrayList to sum each time as opposed to the grades in that list.
rather that line of code should be

sum = sum + grade;

or you can use the shorthand notation

sum += grade;

if you want to test it you can replace this part of your current code (in the getAverage() method)

with this

    //we create two differnt sums to test the two code
	int sum = 0;
	int sum2 = 0;
	
  for(Integer grade : grades) {
    //adding the grades.size()
  	sum = sum + grades.size();
  	
  	//adding the value of grade, the iterator
  	sum2 += grade;
  }
    //printing out the current sum we have accumulated in sum and sum2
    System.out.printf("sum is: %d\nsum2 is: %d\n",sum, sum2);

	int average = sum / grades.size();
	int average2 = sum2 / grades.size();
	
	//printing out the current average we have accumulated in 
	//average and average2
	System.out.printf("average is: %d\naverage2 is: %d\n",average, average2);
    System.out.println(average);

and test it, see the difference.

@arcwhiz56226 were you able to pass with the exercise with this code? Then CodeAcademy would need to re-evaluate their test cases for that exercise