• 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

For Loop Produces Weird Output

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured I would start a new topic on this one. The problem is, I have a for loop that I would think would iterate through each element of an array, however it only grabs the last array.

This is the for loop code



This is my code that generates the array by using methods in another class



When I run this in the command line I get the following output:

0.83
0.83
0.83
the total wholesale is $2.4899999999999998
press the enter key to continue


Notice that is only grabbing from newin[2] and it grabs that number 3 times.



 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's nothing weird here. Again, your Inventory class still has static fields, and each time you create a new Inventory object and fill those fields, you are in effect overwriting the old info. Again, there is no place for statics here as it is most definitely causing your error. As I mentioned in your previous similar thread, get rid of the static modifier here. Don't use it unless you know definitely that you should use it (and here you should not).
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, I guess that lesson on statics wasent over.

That is very cool how that works.

I cant say enough good things about this forum. Thanks a lot.

For reference the following code contained static variables which as Pete said, were overwritten.

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool, I'm glad that you've got this solved.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic