1

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;
 }
asked Jan 17, 2012 at 23:50
2
  • 'unexpected type errors' - What's the exact error message? That probably already answers half your question. Commented Jan 17, 2012 at 23:54
  • It also looks like quantity is a local variable, so you need to declare it somewhere, int quantity; (you may have declared purchase twice above, I assume that int purchase = 0 should be quantity Commented Jan 17, 2012 at 23:55

3 Answers 3

3

Equality checking is done with ==.

answered Jan 17, 2012 at 23:52
Sign up to request clarification or add additional context in comments.

2 Comments

Yes you are absolutely right, its getting late and I'm extremely tired. Thanks!
If your question is answered, feel free to mark the question as answered (by clicking on the 'tick' on the left hand side of my answer).
2

= is the assignment operator, you need two equals signs, == for comparisons.

answered Jan 17, 2012 at 23:52

Comments

0

For your second problem, I think you need:

purchase.setQuantity(quantity);
answered Jan 17, 2012 at 23:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.