5

In my online computer science class I have to write a program to determine the surface gravity on each planet in the solar system. I have gotten almost every aspect of it to work save one. I need to write the surface gravity to a file using a separate method. This is my current method:

public static void writeResultsToFile(double g) throws IOException{

    PrintWriter outFile = new PrintWriter(new File("planetaryData.txt"));

    outFile.printf("%.2f%n", g);
    outFile.close();
}

My problem is that when I write it to a file it will overwrite the previous value. How do I get it include all of the values. Here is the entirety of my code if that helps:

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;


public class gravityV1 {

    /**
     * @param args
     */

    public static double calculateSurfaceGravity(double d, double M){
        double G = 6.67 * Math.pow(10, -11);
        double r = d;
        double g;

        g = G * M/Math.pow(r/2*1000, 2);

        return g;
    }

    public static void printResultsToScreen(String planetName, double diameterKm, double massKg, double g){

        System.out.printf("%-15s%-17.0f%-17.2e%.2f%n", planetName, diameterKm, massKg, g);

    }

    public static void writeResultsToFile(double g) throws IOException{

        PrintWriter outFile = new PrintWriter(new File("planetaryData.txt"));

        outFile.printf("%.2f%n", g);
        outFile.close();
    }

    public static void main(String[] args) throws IOException {
        // Variables
        String [] planetName = new String[8];
        planetName[0] = "Mercury";
        planetName[1] = "Venus  ";
        planetName[2] = "Earth  ";
        planetName[3] = "Mars   ";
        planetName[4] = "Jupiter";
        planetName[5] = "Saturn ";
        planetName[6] = "Uranus ";
        planetName[7] = "Neptune";
        double [] diameterKm = new double[8];
        diameterKm[0] = 4880;
        diameterKm[1] = 12103.6;
        diameterKm[2] = 12756;
        diameterKm[3] = 6794;
        diameterKm[4] = 142984;
        diameterKm[5] = 120536;
        diameterKm[6] = 51118;
        diameterKm[7] = 49532;
        double [] massKg = new double[8];
        massKg[0] = 3.30 * Math.pow(10, 23);
        massKg[1] = 4.869 * Math.pow(10, 24);
        massKg[2] = 5.97 * Math.pow(10, 24);
        massKg[3] = 6.4219 * Math.pow(10, 23);
        massKg[4] = 1.900 * Math.pow(10, 27);
        massKg[5] = 5.68 * Math.pow(10, 26);
        massKg[6] = 8.683 * Math.pow(10, 25);
        massKg[7] = 1.0247 * Math.pow(10, 26);
        double [] g = new double[8];
        int array = 0;

        //code

        System.out.printf("%s%20s%15s%15s%n", "Planet", "Diameter (km)", "Mass (kg)", "g (m/s^2)");

        for(double d : diameterKm){
            g[array] = calculateSurfaceGravity(d, massKg[array]);
            printResultsToScreen(planetName[array], d, massKg[array], g[array]);
            writeResultsToFile(g[array]);
            array++;
        }
    }

}
Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
spenserpro
  • 53
  • 1
  • 1
  • 3
  • 3
    Wrap the print writer around a [FileWriter](http://docs.oracle.com/javase/6/docs/api/java/io/FileWriter.html) and use the append mode - `new FileWriter(String, boolean)` – Perception Jan 21 '13 at 17:06
  • @Perception: I would put that down as an answer. – beny23 Jan 21 '13 at 17:09
  • @beny23 - I would have but I'm pretty sure this question is a duplicate. Still searching for the previous question which I'm sure I've seen on SO before. – Perception Jan 21 '13 at 17:12
  • possible duplicate of [How to append data to a file?](http://stackoverflow.com/questions/6027764/how-to-append-data-to-a-file) – Perception Jan 21 '13 at 17:14
  • Not a dup -- appending is a poor answer – Mel Nicholson Jan 21 '13 at 17:22
  • Possible duplicate of [How to append text to an existing file in Java](http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) – Mad Physicist Nov 19 '15 at 12:45

4 Answers4

14

Do this in order to create a PrinterWriter working with a FileWriter in append mode:

PrintWriter outFile = new PrintWriter(new FileWriter("planetaryData.txt", true)); 
piokuc
  • 25,594
  • 11
  • 72
  • 102
1

From Mkyong's tutorials:

FileWriter, a character stream to write characters to file. By default, it will replace all the existing content with new content, however, when you specified a true (boolean) value as the second argument in FileWriter constructor, it will keep the existing content and append the new content in the end of the file.

You can use something like -

    PrintWriter outputFile = new PrintWriter(new FileWriter(file, true));
Swapnil
  • 8,201
  • 4
  • 38
  • 57
0

Two problems:

1) You are overwriting the file each time. printResultsToFile should take the whole array, not just one datum. Call it from outside your loop.

2) Consider entering your floating point numbers as 2.08e24 for brevity.

private void printResultsToFile(double result[]) {
  PrintWriter pw = new PrintWriter(new FileWriter("planetaryData.txt"));
  for (int i=0; i< result.length; i++) {
    pw.printf("%.2f%n", result[i]);
  }
  pw.close();
}
Mel Nicholson
  • 3,225
  • 14
  • 24
-2

the following code also works to open file name planetaryData.txt.

PrintWriter outFile = new PrintWriter("planetaryData.txt");
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245