Calculating Average from a CSV File

后端 未结 4 1610
感动是毒
感动是毒 2021-01-21 23:35

I have a CSV file, format as follows:

City,Job,Salary Delhi,Doctors,500 Delhi,Lawyers,400 Delhi,Plumbers,100 London,Doctors,800 London,Lawyers,700 London,Plumbers,

4条回答
  •  心在旅途
    2021-01-22 00:25

    br.readLine() before the while-loop will avoid header line problem, but if your data is not correct you will get same Exception again, so, in order to make a safer method you can change this line:

    int sal=Integer.parseInt(country[2]);
    

    With a try-catch block to iterate through entire file even if a value is not a valid number

    int sal;
    try {
        sal=Integer.parseInt(country[2]);
    } catch (NumberFormatException e) {
        // if you want here you can show an error message 
        // to give feedback to the user there is not a valid number
    }
    

提交回复
热议问题