I am working on a complicated computer science lab. I need to create a method called countItem which returns the quantity of a specified item. If the item is not in the list, it should return 0. Cart is an arraylist of Items. Getquantity returns the quantity which is a instance field of the item file.
After building I get two unexpected type errors. Can anyone help me fix this problem? I can post more code if needed. Thanks
EDIt: THE PROBLEM IS SOLVED THANKS SO MUCH!
public int countItem(Item purchase)
{
int purchase = 0;
if(cart.indexOf(purchase) == -1) // error
quantity = 0;
else
purchase.getQuantity() = quantity; //error
return quantity;
}
public boolean removeItem(Item nameofitem)
{
boolean search;
if(cart.indexOf(nameofitem) == -1)
return search;
else
{
cart.remove(cart.indexOf(nameofitem));
search = true;
}
return search;
}
3 Answers 3
Equality checking is done with ==.
2 Comments
= is the assignment operator, you need two equals signs, == for comparisons.
Comments
For your second problem, I think you need:
purchase.setQuantity(quantity);
int quantity;(you may have declared purchase twice above, I assume thatint purchase = 0should be quantity