Note that there are some explanatory texts on larger screens.

plurals
  1. POjava writing certain output to file
    text
    copied!<p>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:</p> <pre><code>[D@642b6fc7 [D@642b6fc7 [D@642b6fc7 [D@642b6fc7 [D@642b6fc7 [D@642b6fc7 [D@642b6fc7 [D@642b6fc7 [D@642b6fc7 </code></pre> <p>How can I fix this? Any help will be greatly appreciated. Below is a snippet of my program:</p> <pre><code>public static double[] calcGravity(double[] radius, double[] mass) { // fill in code here double[] grav = new double[radius.length]; for(int i = 0; i &lt; 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 &lt; 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); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload