This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Trouble with a static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Trouble with a static method

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
I am trying to write a program that calculates the surface gravity on each planet in our solar system. I have the printing of the results assigned to a static method. However, I have a problem. There are two for statements I am using in this program. The last for statement is causing me trouble. It calls back up the printResults static method to use in the main method. However, on the last variable, which is surfaceGravity, the program is not printing out all of the values of surfaceGravity. It is just printing out the last one. How should I get it to print out all of the surfaceGravity values? Can somebody help me with this?

Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874





Use double array for surfaceGravity like you used for others. And index with surfaceGravity[y] to store individually for each y.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

surfaceGravity is declared as an int - which means it can only hold one value. Each time you go through your first loop, you over-write the previous value with the one you compute.

If you want to save all of them, you need an array, just like you store all your other values. Your other option would be to do something like this:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
Thanks for shedding some insight onto my problem. I am continuing to work on it. Thanks
 
Don't get me started about those stupid light bulbs.
 
subject: Trouble with a static method
 
Similar Threads
Enum problem/error
Displaying the largest and smallest of three integers
Need help writing a text file
about enum
Need help with static methods.