A.Ali A.Ali - 2 months ago 11
Java Question

Need help on Java Program to calculate weight on planets

I am doing this program that calculates the weight by planet. I have to display a chart with 3 columns, planet names, Gravity values, and Weight. I am trying to do this program, but I always get the output of the Gravity Values as NaN and 0.0 for all Weight Values.

Here's my code:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.FileWriter;
public class WeightOnPlanetsV1
public static double[] getGravity( ) throws IOException {
Scanner fileScanner = new Scanner(new File("output.txt"));
double[] Gravity = new double[8];
double[] abc = new double[8];
for(int i = 0; i < 8; i++)
abc[i] = fileScanner.nextDouble();
Gravity[i] = abc[i] / 9.79211720754188E34;
return Gravity;
public static double[] calcWeight(){
double earthWeight =100.0;
double[] Weight = new double[8];
double[] Gravity = new double[8];
double[] mass = new double[8];
for(int a = 0; a < 8; a++){
mass[a] = (earthWeight * 433.59237) / Gravity[a];
Weight[a] = mass[a] * Gravity[a];
return Weight;
public static void PrintResults( ) throws IOException{
double[] Gravity = calcWeight();
double[] Weight = new double[8];
String[] planets = new String[8];
planets[0] = "Mercury";
planets[1] = "Venus";
planets[2] = "Earth";
planets[3] = "Mars";
planets[4] = "Jupiter";
planets[5] = "Saturn";
planets[6] = "Uranus";
planets[7] = "Neptune";
Scanner in = new Scanner(System.in);
System.out.print("Please enter your weight in pounds: ");
double weightonEarth = in.nextDouble();
System.out.println(" My weight on the Planets ");
System.out.printf("%10s %9s %8s", "Planet", "Gravity", "Weight on planet(lbs)");
System.out.println("------------------------------------------- -------");
for(int y = 0; y < 8 ; y++)

System.out.printf("%-11s", planets[y]);
System.out.printf("%13.2f", Gravity[y]);


Here are my output values


I will really appreciate any help!


Your gravity values are being calculated from this:

double[] Gravity = calcWeight();

Inside calcWeight(), you return a double[] Weight array which is calculated from:

Weight[a] = mass[a] * Gravity[a];

As @Thomas correctly pointed out, you never actually assign any values to Gravity[] because getGravity() is never called. So the denominator of each Weight value will be zero, i.e. this is a divide by zero which I believe is the cause of the NaN values for Gravity which you are seeing.

Inside PrintResults(), the Weight[] array is never defined, hence they are all zero.

How to fix your code:

You can try using the following:

public static double[] getGravity() throws IOException {
    // same as before

// pass the already-computed gravity array to this method
public static double[] calcWeight(double[] gravity) {
    double earthWeight = 100.0;
    double[] weight = new double[8];
    double[] mass = new double[8];

    for (int a=0; a < 8; a++) {
        mass[a] = (earthWeight * 433.59237) / gravity[a];
        weight[a] = mass[a] * gravity[a];
    return weight;

public static void PrintResults() throws IOException {
    double[] Gravity = getGravity();
    double[] Weight = calcWeight();

    // keep the rest as is