从CSV文件计算平均值 [英] Calculating Average from a CSV File

查看:467
本文介绍了从CSV文件计算平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件,格式如下:

I have a CSV file, format as follows:

城市,工作,薪水
德里,医生,500
德里,律师,400
德里,水管工,100
伦敦,医生,800
伦敦,律师,700
伦敦,水管工,300
东京,医生,900
东京,律师,800
东京,水管工,400
律师,医生,300
律师,律师,400
律师,水管工,500
香港,医生,1800
香港,律师,1100
香港,水管工,1000
莫斯科,医生,300
莫斯科,律师,200
莫斯科,管道工,100
柏林,医生,800
柏林,水管工,900
巴黎,医生,900
巴黎,律师,800
巴黎,水管工,500
Paris,Dog catchers,400

我想找到工资总额的平均值。

I want to find the average of the total salaries.

这是我的代码:

`import java.io。*;

` import java.io.*;

公共类A {

public static void main(String args[])
{
A a= new A();
a.run();
}

public void run()
{
String csv="C:\\Users\\Dipayan\\Desktop\\salaries.csv";
        BufferedReader br = null;
String line = "";
int sum=0;
int count=0;
//String a=new String();


        try {

            br = new BufferedReader(new FileReader(csv));
            try {
                while ((line = br.readLine()) != null) {

                        // use comma as separator
                    String[] country = line.split(",");
                    int sal=Integer.parseInt(country[2]);
                    sum=sum+sal;
                         count++;
                //System.out.println("Salary [job= " + country[0] 
                                  //        + " , salary=" + country[2] + "]");

                }
            } catch (NumberFormatException | IOException e) {
                System.out.println("NA");
                e.printStackTrace();
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }  
        System.out.println(sum/count);


        System.out.println("Done");
      }

    }` 

但是,显示错误:


java.lang.NumberFormatException:对于输入字符串:Salary
at java.lang.NumberFormatException.forInputString(Unknown Source )
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at A.run(A.java:30)
.在A.main(A.java:9)
线程main中的异常java.lang.ArithmeticException:/ by $
at A.run(A.java:46)
在A.main(A.java:9)`

java.lang.NumberFormatException: For input string: "Salary" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at A.run(A.java:30) at A.main(A.java:9) Exception in thread "main" java.lang.ArithmeticException: / by zero at A.run(A.java:46) at A.main(A.java:9)`

是否有更好或更短的代码来解析CSV文件。

Is there any better or short code to parse the CSV file.

推荐答案

第一行包含第三点中的Salary一词。在循环之前放置 br.readLine(),一切都应该没问题。

The first line contains the word "Salary" in the third spot. Put br.readLine()before the loop and everything should be fine.

你有:

br = new BufferedReader(new FileReader(csv));
try {
  while ((line = br.readLine()) != null) {

将其更改为:

br = new BufferedReader(new FileReader(csv));
br.readLine()
try {
  while ((line = br.readLine()) != null) {

这篇关于从CSV文件计算平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆