Java中CSV列的总和

时间:2017-03-03 16:41:42

标签: java excel csv sum

我已将CSV文件导入到我的java应用程序中,并且我正在尝试生成其中一列的SUM。但是,显示的SUM的结果与我在Excel电子表格中计算列的SUM时获得的SUM值不同。

我已经在许多不同的应用程序中检查了总和的结果,除了我的java应用程序之外,它产生了相同的结果。我的CSV文件中有3281行(这可能是一个影响因素)。

    String csvFile = "1722_schools-number-on-roll-warwickshire-1998-2010.csv";
    String line = "";
    String cvsSplitBy = ",";
    int sum = 0; 
    int  year1 = 0;
    double numbersenrolled = 0;

    try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
            br.readLine();

        while ((line = br.readLine()) != null) {

            // use comma as separator
            String[] studentenroll = line.split(cvsSplitBy);
           year1 = Integer.parseInt(studentenroll[9]);
            //debug the fie and each numbers
            sum = sum + year1;
            numbersenrolled++;



            System.out.println("Number of Students Enrolled [Year 1 = " +    year1 + " , Year 2 = " + studentenroll[10] + "]");

        }

            //sum + median + mean
            System.out.println("Sum: " + sum);
            System.out.println("Average: " + (sum/numbersenrolled) );
            System.out.println(numbersenrolled);

    } catch (IOException e) {
        System.out.println("File does not exist");
        e.printStackTrace();
    }

0 个答案:

没有答案