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?

/**
 * This program determines the surface gravity (g) on each planet in our solar system. 
 * 
 * @author John D. Barry 
 * @date  02/17/09
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
public class GravityV1
{   

      public static void printResults(String[] names, double[] diameter, double[] mass, int x, double surfaceGravity)
      {
      System.out.printf("%7s%35.2f%35.4e%35.2f\n",names[x],diameter[x],mass[x],surfaceGravity);
      }

    //main method
    public static void main (String [ ] args)

    {

        double surfaceGravity = 0.0;
        double[] mass = { 3.30E23, 4.869E24, 5.972E24, 6.4219E23, 1.900E27, 5.68E26, 8.683E25, 1.0247E26, 1.27E22 };
        double[] diameter = { 4880000, 12103000.6, 12756000.3, 6794000, 142984000, 120536000, 51118000, 49532000, 2274000 };
        String[] names = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
        
        for (int y = 0; y < 9; y++)
        {
            surfaceGravity = (((6.67E-11) * (mass[y])) / (Math.pow(diameter[y] / 2, 2)));
            System.out.printf("%25.2f\n", surfaceGravity);

        }
        
        for (int x = 0; x < 9; x++) 
        
       
        {
            printResults(names,diameter,mass,x,surfaceGravity);
        }

        }
}

From what I see, unlike the rest of the variables (mass, diameter and names which are all arrays) surfaceGravity is just a normal variable.

Hence the following for loop:-

for (int y = 0; y < 9; y++) {
  surfaceGravity = (((6.67E-11) * (mass[y])) /(Math.pow(diameter[y] / 2, 2)));
  System.out.printf("%25.2f\n", surfaceGravity);
}

Actually does nothing but print the values of surfaceGravity from 0-9.
However the value of surfaceGravity variable are overwritten in the next iteration of the for loop. i.e. for the first iteration your loop calculates and stores the value of surface gravity for the first planet in variable "surfaceGravity", in the second iteration the we calculate the surface gravity for the second planet and store in the variable "surfaceGravity" (as a result the value for the surface gravity for the first planet is lost, overwritten by this new value) this goes on till the nineth planet (which is where the "for" loop terminates hence its value is not overwritten), and hence "surfaceGravity" has the value for only of the nineth planet.

My suggestion from the way your program has been designed is move the call to the "printResults()" method inside the second last "for" loop in your main.

This question has already been answered. Start a new discussion instead.