Question: Hi I have one problem with my APCS program. I have to save my results, my weight data, to a text file at the end

Hi I have one problem with my APCS program. I have to save my results, my weight data, to a text file at the end of my program. I have code to read a previous text file, but i do not know how to save my data on to a new one. Below is my code. Thank you!

import java.util.Scanner;

import java.io.File;

import java.io.PrintWriter;

import java.io.IOException;

public class planetWeight

{

public static void main(String[] args) throws IOException

{

String [] planet = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};

double mass = 0;

double inputWeight = 0;

Scanner in = new Scanner (System.in);

System.out.print("Please enter your weight in pounds: ");

double[] gravity = gravity();

inputWeight = in.nextDouble();

mass = (inputWeight * 453.59237) / gravity[2];

double planetGravity[] = gravity();

double userWeight[]= massToWeight(planetGravity, mass);

output(planetGravity, userWeight, planet);

}

public static double[] massToWeight(double[] planetGravity,double mass)

{

double[] userWeight = new double [8];

for (int i = 0; i < 8; i++)

{

userWeight[i] = (mass * planetGravity[i]) / 453.59237;

}

return userWeight;

}

public static void output( double[] planetGravity, double[] weight, String[] planetName)

{

System.out.printf(" %11s%n", "My Weight on the Planets");

System.out.println("Planet Gravity Weight(lbs)");

for (int i = 0; i < 8; i++)

{

System.out.printf(planetName[i] + " %-9.2f %-6.2f", planetGravity[i], weight[i]);

System.out.println("");

}

}

public static double[] gravity()throws IOException

{

int i = 0;

double[] planetGravity = new double[8];

File fileName = new File("gravity1.txt");

Scanner inFile = new Scanner(fileName);

while (inFile.hasNext())

{

planetGravity[i] = inFile.nextDouble(); i++;

}

inFile.close();

return planetGravity;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!