• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please somebody take me over this code segment

 
Ranch Hand
Posts: 43
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 30
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
caleb momanyi
Ranch Hand
Posts: 43
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
S.R Paul
Ranch Hand
Posts: 30
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 43
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much, I was beginning to feel bad that I was annoying the community. so why the code not working?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
caleb momanyi
Ranch Hand
Posts: 43
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic