alphamonkey
alphamonkey

Reputation: 249

JAVA Calculating Weight on Each Planet (beginner)

I need to write a program that reads a list of data of surface gravities (of all the planets) from a file, and calculate my weight in pounds on each planet. I need to display the data in a formatted table.

This is what I have so far::

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

public class WeightOnPlanetsV1
{
public static void printHeading(){
System.out.println("      My Weight on the Planets     ");
System.out.printf("%9s%13s%15s", "Planet", "Gravity", "Weight(lbs)");
System.out.println("\n  ------------------------------------");


}

public static double [] readFile() throws IOException{
     double[] gravity = new double [7];  
     Scanner inFile = new Scanner(new File("gravitydata.txt"));
     int count = 0;
     while (inFile.hasNext()){
        double temp = inFile.nextDouble();
        gravity[count] = temp;
        count++;
     }
  inFile.close();
  return gravity;
}

public static double[]calculateWeight()throws IOException{
   double[] gravity = new double [7];
   readFile();
   double[] mass = new double[7];
   double[] weight = new double[7];

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

public static void main (String[] args)throws IOException{
  double[] gravity = new double [7];  
  printHeading();
  String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
  calculateWeight();
  double [] weight = calculateWeight();          
  for(int y = 0; y < 9 ; y++)
  {
     System.out.printf("%-11s", names[y]);     
     System.out.printf("%13.2f", gravity[y]); 
     System.out.printf("%20.2f\n",weight[y]);
 }
} 
}     

When I run the code, the only thing that shows is the header, and there is an Array Out of Bounds Error. There are 8 planet gravities in the file, so I'm not sure exactly what is wrong. I have tried editing all sorts of things, but I cannot get any further. I would appreciate it if someone could help me figure out what is wrong with the code.

Upvotes: 0

Views: 2101

Answers (2)

turingcomplete
turingcomplete

Reputation: 2188

There are a few things that you need to fix. I added comments in the code

public static double [] readFile() throws IOException{
     double[] gravity = new double [8]; //size 8 instead of 7  
     Scanner inFile = new Scanner(new File("gravitydata.txt"));
     int count = 0;
     while (inFile.hasNext()){
        double temp = inFile.nextDouble();
        gravity[count] = temp;
        count++;
     }
     inFile.close();
     return gravity;
}

public static double[]calculateWeight(double [] gravity)throws IOException{
   double[] mass = new double[8]; //size 8 instead of 7
   double[] weight = new double[8]; //size 8 instead of 7

   for (int a = 0; a < 8; a++){ //loop 8 times, not 9
     mass[a] = (110*433.59237)/(gravity[a]);
     weight[a] = (mass[a]*gravity[a]);
     //a++; you don't need to increment a here, since it's incremented in the for loop
   }
   return weight;
}

public static void main (String[] args)throws IOException{ 
  printHeading();
  String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
  //calculateWeight(); no need to call it twice
  double [] gravity = readFile();
  double [] weight = calculateWeight(gravity);          
  for(int y = 0; y < 8; y++)//loop 8 times, not 9
  {
     System.out.printf("%-11s", names[y]);     
     System.out.printf("%13.2f", gravity[y]); 
     System.out.printf("%20.2f\n",weight[y]);
  }
} 

Upvotes: 1

Dylan Meeus
Dylan Meeus

Reputation: 5802

You say

double[] gravity = new double [7];  

but you access

 System.out.printf("%13.2f", gravity[y]); at position 8

Upvotes: 0

Related Questions