Private Access in ArrayList Error


#1

I continue to get:

GradeAnalyzer.java:8: error: size has private access in ArrayList
if (grades.size < 1) {
^
GradeAnalyzer.java:18: error: cannot find symbol
int average = sum / grades.size;
^
symbol: variable sum
location: class GradeAnalyzer
GradeAnalyzer.java:18: error: size has private access in ArrayList
int average = sum / grades.size;
^
3 errors

I’m unsure of what to do in order to give it access.
Here is my code:

import java.util.ArrayList;
class GradeAnalyzer{
  public GradeAnalyzer(){}
	public int getAverage(ArrayList<Integer> grades){
    if (grades.size < 1) {
      System.out.println("Array List 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<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

you attempt to access a private property, which you can’t do. Private properties can only be accessed within the class itself.

Use the size method:

https://stackoverflow.com/questions/19450846/size-has-private-access-in-arraylist


#3

Now I’m getting:

GradeAnalyzer.java:19: error: cannot find symbol
int average = sum / grades;
^
symbol: variable sum
location: class GradeAnalyzer
1 error


#4

i helped you with the error you where stuck at, don’t you want a moment to delve into this new error first?

Now you have a scope problem, variable (like sum) have a block scope. Here some information in that regard:

https://www.geeksforgeeks.org/variable-scope-in-java/


#5

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