1

I am trying to program something in java because google sheets is over-complicating things for me. (I have a project for school that has to do with the population of China, if you were wondering what the statistics are.) The variable output[] apparently was not initialized. As far as I'm concerned, everything should work (the program is meant to output the difference of the population between years).

import java.util.*;
import java.lang.*;
import java.io.*;

class ActualStatistics{

    public static void main(String[] args){
        double[] year = {1960,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,1971,1972,1973,1974
        ,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1989,1990,1991,1992
        ,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013};
    double[] population = {667300000,667300000,660300000,665800000,682300000,698400000,715200000,735400000,754600000,774500000,796000000,818300000,841100000,862000000,881900000,900400000,916400000,930700000,943500000,956200000,969000000,981200000,993900000,1009000000,1023000000,1037000000,1051000000,1067000000,1084000000,1102000000,1119000000,1135000000,1151000000,1178000000,1192000000,1205000000,1218000000,1230000000,1242000000,1253000000,1263000000,1272000000,1280000000,1288000000,1296000000,1304000000,1311000000,1318000000,1325000000,1331000000,1338000000,1344000000,1351000000,1357000000};
        System.out.println("Year   |   Population");
        for(int counter=1;counter<=population.length;counter++){
            double[] output;
            output[counter] = population[counter+1] - population[counter];
            System.out.println(year[counter]+"   |   "+output[counter]);
        }
    }
}
  • `double[] output` declares the variable but does not initalize it. Try `double [] output = new double [population.length];` – Eric G Jan 06 '16 at 18:56

1 Answers1

2

You get the error because you did not initialize the array:

double[] output = new double[SIZE];

Note that you don't really need an array in this case:

for(int counter=1;counter<=population.length;counter++){
    double output;
    output= population[counter+1] - population[counter];
    System.out.println(year[counter]+"   |   "+output);
}
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67