2014-11-16 64 views
1
public class WeightOnPlanetsV1 
{ 
    public static double[] calcWeight(double[] gravity, double[]mass) 
    { 
    double[] weight = new double[gravity.length]; 

    for (int i = 0; i < gravity.length; i++) { 
     weight[i] = (mass[i] * 1000)/gravity[i]; 
     weight[i] = weight[i]/433.59237; 
    } 

    return weight; 
    } 

    public static double[] takeFromFile(double[] gravity)throws IOException 
    { 
    File fileName = new File("GravityResults.txt"); 
    Scanner inFile = new Scanner(fileName); 

    for (int i = 0; i < gravity.length; i++) { 
     gravity[i] = inFile.nextDouble(); 
     gravity[i] = gravity[i]/10; 
    } 
    inFile.close(); 

    return gravity; 
    } 

    public static void printResults(String[] names, double[] gravity, double weight[]) 
    { 
    System.out.printf("%37s \n","My Weight on the Planets"); 
    System.out.printf("%5s %20s %15s \n","Planet","Gravity","Weight(lbs)"); 
    System.out.println("---------------------------------------"); 
    for (int i = 0; i < names.length; i++) { 
     System.out.printf("%5s %10f %15f \n",names[i], gravity[i], weight[i]); 
    } 
    } 

    public static void main(String[] args)throws IOException 
    {   
    String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}; 
    double[] weightOnPlanets = {100, 100, 100, 100, 100, 100, 100, 100, 100}; 
    double[] gravity = {}; 
    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)}; 

    double[] gravities = takeFromFile(gravity); 
    double[] finalWeights = calcWeight(gravities,masses); 

    printResults(names, gravities, finalWeights); 
    } 
} 

我的錯誤獲取ArrayOutofBoundsException來自在我的for循環

for (int i = 0; i < names.length; i++) { 
    System.out.printf("%5s %10f %15f \n",names[i], gravity[i], weight[i]); 
} 

當我嘗試打印結果。它給了我

java.lang.ArrayIndexOutOfBoundsException: 1 
    at WeightOnPlanetsV1.printResults(WeightOnPlanetsV1.java:45) 
    at WeightOnPlanetsV1.main(WeightOnPlanetsV1.java:63) 

的錯誤

+0

當你得到這個異常時,你是否嘗試在Eclipse的控制檯上打印異常? –

回答

1

基本上,你初始化: double[] gravity = {};在主函數。

然後調用:double[] gravities = takeFromFile(gravity);由於重力沒有元素,因爲在你的takeFromFile(雙[])功能你有這樣的:

for (int i = 0; i < gravity.length; i++) { 
    gravity[i] = inFile.nextDouble(); 
    gravity[i] = gravity[i]/10; 
} 

基本上讀取文件gravity.lenght times(這是一個嚴重的壞主意,順便說一句,如果文件數量少於重力[)

不管怎麼說,正如有人早些時候說,這一切都導致重力和重力是空的,所以不會finalWeights [],因爲你使用初始化它:

double[] finalWeights = calcWeight(gravities,masses);

,自比重有0元素......好吧,那發生了。 現在,名[]有9個元素,這是大於0,所以這就是爲什麼在:

System.out.printf("%5s %10f %15f \n",names[i],gravity[i], weight[i]);

你會得到一個ArrayOutOfBounds異常(因爲你沒有檢查重力[]重量[]大小,並且都變成了0)。這意味着你需要確保初始化重力[]具有較大lenght名稱[]

我個人會重寫代碼。創建一個名爲「地球」的對象與質量重力屬性和對象,它確實與您的函數中的函數calcWeight()calcWeight(雙質量)。然後,你可以在你的主函數中創建一個星球,並且還可以使用它在takeFromFile(Planet [] planets),通過文件初始化所有行星數組而不僅僅是權重。但這只是我的偏好。

3

您檢查約束names陣列和訪問gravityweight假設他們有相同或更大的長度names

+0

是的,我看到這可能是問題。感謝您的問題。 –

1

你的問題takeFromFile接收開始一個空數組(double[] gravity = {};)並返回一個空數組。

後來所有迭代gravity(或gravities)陣列的環什麼也不做,而循環,迭代從0到names.length - 1(在printResults)導致ArrayIndexOutOfBoundsException,因爲它假設namesgravity陣列具有相同的長度。

0

double[] gravity = {};是一個空數組,所以你必須填寫它像其他陣列

0

修改你的代碼的對象&名單的工作。它變得更加清晰:

class Planet { 
    String name; 
    double weightOnPlanet; 
    double gravity; 
    double mass; 
    public Planet(...) { ... } // Constructor. 
} 

public static void main(String[] args) throws IOException { 
    List<Planet> planets = new ArrayList<>(); 
    planets.add(new Planet(...)); 
    ... 
}