Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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

Attempting to reset a loop/ method

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have the following code which outputs the total value of all inventory in my program. However the problem is, it does not get reset each time the method is called.

For example it will output:

$500 the first time, which is correct
$1000 not correct
$1500 the third time which shows it is always adding on to itself, and not doubling.



Is there anyway to ensure that this mehtod is reset so the next time it is called, it starts from 0
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
something else is going on besides your loop not being reset. The fact that you have declared i and set it to start with 0 in the for condition should cause it to start fresh each time the method is called. Also, once the loop is started there does not appear to be anything that changes the value of i value other then each iteration of the loop.
 
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
The solution is to add sumWhole=0; as the first line of code.

Not sure why i dident try that first, instead of last...
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ahhh, OK, that makes sense, I clearly missed the point of your original post.

Still, something doesn't feel right. the variable sumWhole is not declared within the method, so I guess it is an instance variable of the class this method is in.

Now, you are calling this method either from within this class, or from another class. Which is it?

So your method resets an instance variable each time it is called, then applies some math to it, then returns the instance variable, I guess there is a good reason for that, but it makes me think about taking a second look at how your variables are defined, maybe sumWhole should be a local variable?

Then again maybe it is ok, I guess it depends on the program.

cheers.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, that always remains the problem with globals. Here as far as I can see, sumWhole is a class level variable and lives till the lifetime of that object.

As per your requirement, sumWhole need not be a class level variable. It should be a local variable in the method : getWhole()

Regards
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic