Q:

java writing certain output to file

I have a question on how to write a certain part of a program's output to a file. In my case, my program displays the radius, mass, diameter, and gravity of all the planets and I just need to display the gravity in a file. When I run my program, the file is created, but all that is displayed in it is this:
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7
[D@642b6fc7

How can I fix this? Any help will be greatly appreciated. Below is a snippet of my program:
public static double[] calcGravity(double[] radius, double[] mass)
{
    // fill in code here
        double[] grav = new double[radius.length];
        for(int i = 0; i < radius.length; i++){
            grav[i] = (6.67E-17) * mass[i] / Math.pow(radius[i], 2);
        }
        return grav;
    // The formula to calculate gravity is:
    // 6.67E-17 times the massOfPlanet divided by the radius of the planet squared
}
//print the gravity values to text file
public static void printToFile(double[] gravity)throws IOException
{
    // fill in code here
        PrintWriter outFile = new PrintWriter(new File("/Users/timothylee/gravity1.txt"));
        for(int i = 0; i < gravity.length; i++){
            outFile.println(gravity);
        }
        outFile.close();
    }

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 [] mass = {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);

A:

When you print out something that isn't a primitive data type (an Object, an array, etc.) the memory address of the data structure is printed instead of the contents you are looking for.  In the case of an array, you want to iterate through all of its elements and print them one by one.
In the case of an Object, you need to call the getter functions for the instance variables you want to print.  For example:
public class ExampleObject {
    private int x;
    public void setX(int x) { this.x = x; }
    public int getX() { return x; }

    public static void main(String[] args) {
        ExampleObject ex = new ExampleObject();
        ex.setX(5);
        System.out.println(ex); // prints the memory address of this ExampleObject
        System.out.println(ex.getX()); // call the getter for what you want to print.
    }
}

However, if you want to print x by default when you run the command System.out.println(ex) rather than the memory address, you can override the Object class' toString() method, which is the method called to get the string representation of a class when you ask to print an object.  By default it returns the memory address of the object, which is why that is what you see.
public class ExampleObject {
    private int x;
    public void setX(int x) { this.x = x; }
    public int getX() { return x; }
    public String toString() { return ""+x; }

    public static void main(String[] args) {
        ExampleObject ex = new ExampleObject();
        ex.setX(5);
        System.out.println(ex); // prints the string representation of ex, which is the value of x
    }
}

