Originally posted by Mark Vedder:
The code you show will always remove the first item. Your enumeration hits the first element, removes it, and then breaks out of the enumeration. Instead, you want to iterate through the enumeration and get the next product as you are doing, but only delete it if its id matches the id received as a parameter, and then break out of iterating through the enumeration (if the id doesn't match, you don't delete the current element, or break out of the enumeration; instead just loop through to the next element).
It should be noted that iterating through a shopping cart like that to remove an item is very inefficient. What type of collection (i.e. what class) are you using for your Shopping Cart? Take a look through the various collection classes in the Java API; you can learn about them via the Sun Tutorial on the Collection Framework. You should see a collection type that is geared towards this type of thing, and will allow you to add and remove items in a very efficient manner, without needing to iterate through them.
Originally posted by Ben Souther:
If you put the code tags around your code, the indentation will be preserved and it will be much easier for us to read.
You're using the == operator to compare strings.
You should use the equals() method of String.
You were but you switched.
The '==' operator, when comparing object variables, will return true only if both variables point to the same object. Since you're using trim (which returns a new string) on your strings, it will always return false.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |