I am a scala beginner and I face a problem when I am doing my homework because there is null in the text file for example (AFG,Asia,Afghanistan,2020-02-24,1.0,1.0,,,,,0.026,0.026). So, I need to replace the null in array with zero. I read the scala array members and try the update and filter method but I cannot solve it. Can someone help me?
val filename = "relevant.txt" //Access File
val rf = Source.fromFile(filename).getLines.toArray //Read File as Array
for(line <- rf){ //for loop
line.(**How to replace the null with 0?**)
//println(line.split(",")(0))
if (line.split(",")(0).trim().equalsIgnoreCase(iso_code)){ //check for correct ISO CODE , ignores the casing of the input
total_deaths += line.split(",")(4).trim().toDouble //increases based on the respective array position
sum_of_new_deaths += line.split(",")(5).trim().toDouble
record_count += 1 //increment for each entry
}
}
AFG,Asia,Afghanistan,2020-02-24,1.0,1.0,,,,,0.026,0.026becomeAFG,Asia,Afghanistan,2020-02-24,1.0,1.0,0,0,0,0,0.026,0.026?val cleanLine = ",\\s*(?=,)".r.replaceAllIn(line,",0")