Search code examples
javaarraylistbluejgetmethod

How to fix my getTotalInventoryCount()? bad operand types for binary operator?


Hi sorry beginner coder here and I am not good at explaining things very well but have been getting this error code that states: bad operand types for binary operator '+', first type: int; second type:java.util.ArrayList. Then it highlights the totalIC = totalIC + l.getInventory(); part below. I am not sure how to fix this issue, also I am also using BlueJ compiler. Here is my code that I am having issues with:

public int getTotalInventoryCount()
{

    int totalIC = 0;
    int cars = 0;

    for(LamborghiniCarLot l : carLots){
        if(l.getInventory().equals(getCarLots())){
            totalIC = totalIC + l.getInventory();
            cars++;
        }
    }
    return cars;
}

Here is where I am getting my l.getInventory(); from:

public ArrayList<Lamborghini> getInventory()
{
    return inventory;
}

These are the methods I am suppose to be using:

public int getTotalInventoryCount()

  • Using a foreach loop, gets the total inventory of car lots
  • Returns 0 if carLots ArrayList is null

If anyone can help me with coding this that would be greatly appreciated. If you need more information please let me know. Thank you in advance.


Solution

  • You can not add an ArrayList to an int.

    totalIC = totalIC + l.getInventory();
       ^         ^            ^
      int       int       ArrayList containing Lamborghini-Objects
    

    I assume you want to know how many Lamborghini-Objects are contained in the inventory.

    To do so, you would call

    l.getInventory().size();