Masu_SMB
Masu_SMB

Reputation: 39

Bad operand types for binary operator '+' because of arraylist type

So, I have java as my course this semester and I am facing much problems while learning it. Here, I have to write a program to calculate average of arraylist elements. I have all classes written (as according to requirement of question) and the error lies here so far. Can anyone help me with it?

public int aveScores(ArrayList<ScoreInfo> sList)
{
    int sum = 0;
    if(!sList.isEmpty())
    {
        for(ScoreInfo s : sList)
        {
            sum += s;
        }
    }
    return sum/ sList.size();
}

error link https://i.sstatic.net/QHl1U.png

Upvotes: 2

Views: 310

Answers (1)

Eran
Eran

Reputation: 393936

ScoreInfo is not a numeric type, so you can't add s to your sum.

You probably need something like :

    for(ScoreInfo s : sList)
    {
        sum += s.getScore(); // assuming ScoreInfo class has a getScore method
                             // that returns an int
    }

Upvotes: 1

Related Questions