aspose file tools*
The moose likes Java in General and the fly likes please somebody take me over this code segment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "please somebody take me over this code segment" Watch "please somebody take me over this code segment" New topic
Author

please somebody take me over this code segment

caleb momanyi
Ranch Hand

Joined: Jun 17, 2012
Posts: 43

public synchronized void addItem(Product product){
boolean newItem = true;
for(shoppingCartItem scItem:items){
if(scItem.getProduct().getId()==product.getId;
newItem = false;
shoppingCart.incrementQuantity();

i dont get the part if for() loop please what is the meaning of that expression?
S.R Paul
Ranch Hand

Joined: Mar 22, 2012
Posts: 30

It is the "Enhanced for loop".
It makes the looping easier for the collections and Arrays.

Here how it works;
for(shoppingCartItem scItem:items)...

ShoppingCartItem is a user defined class.
scItem is the reference of the object, which will be assigned while looping (the object will be retrieve from the collection "item")
items is the reference of the object, which is a Collection and type of ShoppingCartItem (mostly)

You can read the segment like this,
"for each object (ShoppingCartItem type)in the collection item, retrieve it and assign it to the scItem reference and do some work inside the loop body"

I hope it helps.


=-- Anything less than immortality is a complete waste of time --=
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7051
    
  16

caleb momanyi wrote:i dont get the part if for() loop please what is the meaning of that expression?

Neither do I, because the code, as you've supplied it, is wrong (it won't even compile).

Please have a look at it again, and read the UseCodeTags page thoroughly before you re-submit.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
caleb momanyi
Ranch Hand

Joined: Jun 17, 2012
Posts: 43

thank you paul it helped a lot.
@Winstone that's not the whole of the code. I left out the rest of it because i was intrested with the explanation for this part only. But it works.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

caleb momanyi wrote:thank you paul it helped a lot.
@Winstone that's not the whole of the code. I left out the rest of it because i was intrested with the explanation for this part only. But it works.


Don't want to offend you but Winston is right. the code snippet you wrote above is wrong. it won't compile. see for yourself in the if syntax. secondly you should follow forum rules and if somebody tell you to put code tags it is for your own good , so that your question can be answered quickly and clearly. code which is not properly formatted won't attract ranchers to answer it. so please in future, pay attention to these things.


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
S.R Paul
Ranch Hand

Joined: Mar 22, 2012
Posts: 30

thank you paul it helped a lot.

That's fine, but next time more clear code.
Anyway, Welcome to the Ranch.
caleb momanyi
Ranch Hand

Joined: Jun 17, 2012
Posts: 43

you were right my friend, there's something wrong with the code in that whenever I add a new cart to a session and try to add a new product I still end up without a product in the cart.
following the flow of code in the debugger I realized that a new product variable is seen just befor the for loop but not added to cart. I don't know where the problem is
caleb momanyi
Ranch Hand

Joined: Jun 17, 2012
Posts: 43

thank you very much, I was beginning to feel bad that I was annoying the community. so why the code not working?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

You should read the web app's logs, or if you haven't started using logging yet, read the System.out and / or System.err output files. You have a runtime error that occurs in the loop which only happens when there is a new item and prevents the loop from completing. The error will throw a runtime exception and, assuming you don't catch it yourself, would be caught and logged by your application server.

I won't tell you what the exception is, because learning to find and use your logs is key to making functional code for a web application.

And if all else fails, write a simple local test which does not use a web application but instead just makes a cart and adds some items to it to see what happens (that is the best part about using POJOs in web applications, you can test them without the web server).


Steve
caleb momanyi
Ranch Hand

Joined: Jun 17, 2012
Posts: 43

thank you all for the quick replis. I finally figured it out. in the this method


the commented part should be outside the for loop. I took it out and got the cart working.
thanks to all who replied
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: please somebody take me over this code segment
 
Similar Threads
Why this method behaves differently?
how to deal with this error???
Problem populating a newly created HashMap in a Struts2 Action
Please critique my countOccurrence method for IntTreeBag class
out of memory exception