This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes For Loop Produces Weird Output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For Loop Produces Weird Output" Watch "For Loop Produces Weird Output" New topic
Author

For Loop Produces Weird Output

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
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.



pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jun 29, 2009
Posts: 84
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

Joined: Feb 23, 2007
Posts: 1561
Cool, I'm glad that you've got this solved.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For Loop Produces Weird Output