Everything in my program seems to work, except I cannot print the gravity values to a separate file. I've bolded the relevant data.



public class GravityV1
{

public static double[] calcGravity(double[] radius, double[] mass)
{
double[] gravity = new double[9];
double g = 6.67E-17;
for(int i = 0; i < radius.length; i++)
{
gravity[i] = ((g * mass[i])/(Math.pow(radius[i],2)));
}
return gravity;
}

// printResults will print the table to output screen
// return type is voide because no values are returned
public static void printResults(String[] name, double[] radius, double[] mass, double gravity[])
{
System.out.println(" Planetary Data");
System.out.println();
System.out.printf("%7s%15s%15s%15s\n"," Planet ","Radius (km)", "Mass (kg)","g (m / s^2)");
System.out.println("-------------------------------------------------------");
}

//print the gravity values to text file
public static void printToFile(double[] gravity)throws IOException
{
PrintWriter outFile = new PrintWriter(new File("GravityData.txt"));

outFile.printf(".2f\n", gravity);

}

public static void main(String[] args)throws IOException
{
// Initialize variables
String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
double[] radii = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
//double[] masses = {3.3022 * Math.pow(10,23), 4.8685 * Math.pow(10,24), 5.9736 * Math.pow(10,24), 6.4185 * Math.pow(10,23), 1.8986 * Math.pow(10,27), 5.6846 * Math.pow(10,26), 8.6810 * Math.pow(10,25), 1.0243 * Math.pow(10,26), 1.312 * Math.pow(10,22)};
// or using big E notation:
double [] masses = {3.30E23, 4.87E24, 5.97E24, 6.42E23, 1.90E27, 5.68E26, 8.68E25, 1.02E26, 1.27E22}; // See IMACS double lesson for big E notation

// Processing
double[] gravities = calcGravity(radii, masses);

// Output
printResults(names, radii, masses, gravities);
printToFile(gravities);
for(int i = 0; i < names.length; i++)
{
System.out.printf(" %-7s", names[i]);
System.out.printf(" %11.0f", radii[i]);
System.out.printf(" %15.2e", masses[i]);
System.out.printf("%10.2f\n", gravities[i]);
}

} //end main
}//end class