File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic

Trouble with a static method

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 84
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

Joined: Oct 02, 2003
Posts: 11955

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: 84
Thanks for shedding some insight onto my problem. I am continuing to work on it. Thanks
I agree. Here's the link:
subject: Trouble with a static method
It's not a secret anymore!