0

I need to read a csv file with blanks, in java, to get sum, average, min, max values of each column.

The below code works only when a csv file doesn't have blanks. What should I do in java when handling a file with blanks? Is there a way of repacing(filling in) blanks with 0, which would not affect other values of each column?

There is a blank before '876' in this csv file as attached
So errors come up saying as below:

java.lang.NumberFormatException: For input string: ""876"" at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054) at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.base/java.lang.Double.parseDouble(Double.java:549) at first.SchoolTeacher1.main(SchoolTeacher1.java:32)

public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\USER\\education.txt"));
    
    String line;        
    ArrayList<String> SchoolData = new ArrayList<>();
    
    try {
    while ((line = reader.readLine()) != null) {
        String[] InputSplited = line.split(",");
        Collections.addAll(SchoolData, InputSplited);
    }   
    
    //1         
    double SumOfSchoolTeacher = 0;
    double min_SchoolTeacher = Double.MAX_VALUE;
    double max_SchoolTeacher = 0;
    
    for (int i = 1; i < SchoolData.size() / 34; i++) {
        SumOfSchoolTeacher += Double.parseDouble(SchoolData.get((i * 34) + 3));
        
         if(Double.parseDouble(SchoolData.get((i * 34) + 3)) >  max_SchoolTeacher) {
             max_SchoolTeacher = Double.parseDouble(SchoolData.get((i * 34) + 3));
            }

         if(Double.parseDouble(SchoolData.get((i * 34) + 3)) < min_SchoolTeacher){
             min_SchoolTeacher = Double.parseDouble(SchoolData.get((i * 34) + 3));
            }
    }

    System.out.println("sum: " + SumOfSchoolTeacher);
    System.out.println("avg: " + SumOfSchoolTeacher / 190);
    System.out.println("Max: " + max_SchoolTeacher);
    System.out.println("Min: " + min_SchoolTeacher);

enter image description here

Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
3489
  • 1
  • 1
  • 1
    Better use existing libraries like [Apache commons](https://commons.apache.org/proper/commons-csv/user-guide.html) to avoid many errors. – the Hutt Mar 31 '21 at 13:35
  • You just need to [strip()](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#strip()) or [trim()](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#trim()) each element in `SchoolData` before you parse it to a number. By the way, why all these [magic numbers](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad), i.e. 3, 34 and 190? Also, consider using [java naming conventions](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html). – Abra Mar 31 '21 at 15:31
  • onkar ruikar and Abra, thanks for your comments! Will try in these ways. Thanks again :) – 3489 Apr 01 '21 at 08:54
  • (To Abra) About those numbers you mentioned, I need certain columns to calculate values. I read a csv file which has 34 columns and 191 lines (but the first line includes column names or explanation, not numbers). And one of the columns I was working on is the 4th column, which means its index(position) is 3 since the first column is at 0 as an index number. – 3489 Apr 01 '21 at 09:02

0 Answers0