0

This program calculates the surface gravities of 9 planets, prints the data to the screen in a formatted table, and is supposed to print only the surfaceGravity values to a file. I'm not sure why my code won't print the calculated surfaceGravity value to the file called gravities2.txt. Can someone please provide some enlightenment? Is it maybe because the gravity data is not put into an array- it's just values that get looped through?

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

public class GravityV1
{   
   //print the data to screen
   public static void printResults(String[] names, double[] diameter, double[] mass, int x)
   {
     System.out.printf("%-10s%21.2f%20.4e",names[x],diameter[x],mass[x]);   
   }

   //calculate the surface gravity
   public static double calcGravity(double m, double d)throws IOException
   { 
      double gravity = 0.0;    

      gravity = (((6.67E-11) * (m)) / (Math.pow((d) / 2, 2)));

      printToFile(gravity);     

      return gravity;        
   }

   //writes gravity data to a text file
   public static void printToFile(double gravity)throws IOException
   { 
       File gravities = new File("gravity2.txt"); 

       PrintWriter outFile = new PrintWriter(gravities);

       outFile.printf("%2.2f\n", gravity);
   }

   //main method
   public static void main (String [ ] args) throws IOException
   {        
      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" };

      System.out.printf("%-7s%20s%20s%20s\n","Planet Name","Diameter","Mass(kg)", "g (m/s^2)");    
      System.out.println("=============================================================================");

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

         double surfaceGravity = calcGravity(mass[x], diameter[x]);

         System.out.printf("%17.2f\n", surfaceGravity);         
      }  

   System.out.println();
  }  
}

The output to the screen is correct, but unfortunately the file contains nothing. The file does get created, so I suppose that's one good thing... Any help is very greatly appreciated!!

4

3 回答 3

4

您需要关闭并冲洗PrintWriter. 这是因为 PrintWriter “缓冲”您的输出,或保存它,并且仅在保存相当数量的情况下才写入(这有助于您的程序运行得更快,因为它会导致更少的大写入而不是大量的小写入)。关闭或刷新文件会强制它写入。

在函数末尾添加outFile.close()会有所帮助,尽管您应该尝试重构代码,这样您就不必不断地创建新的编写器,并且可以在完成后简单地关闭一个编写器!

于 2013-11-11T04:37:30.127 回答
1

两个问题:

1 - 你永远不会关闭PrintWriter. outFile.close()写完后需要调用。

2 - 由于您printToFile在 main 方法的循环中调用该方法,并且该方法PrintWriter每次调用时都会创建一个新对象,因此您最终会得到一个仅包含循环中 LAST 行星的文件。

为了解决最后一个问题,我会在创建PrintWriter循环之前创建一个,并在printToFile每次调用它时将其传递给方法。close()在循环之后记住它。

于 2013-11-11T04:39:27.423 回答
0

在调用 printf 方法时尝试添加刷新。

outFile.flush();
于 2013-11-11T04:38:28.243 回答