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