他们希望我们结合使用ArrayList(或常规数组)和Map Collections Hashmap或Tree Map之一。转换将涉及逐行读取数据文件到适当的结构中。使用称为read_records的方法执行此操作。填充结构后,将其发送到名为print_records的方法,该方法将以以下格式将其打印出来:
"ABW" : {
"2020-03-13" : {
"iso_code" : "ABW",
"location" : "Aruba",
"date" : "2020-03-13",
"total_cases" : "2",
"new_cases" : "2",
"total_deaths" : "0",
"new_deaths" : "0",
"total_cases_per_million" : "18.733",
"new_cases_per_million" : "18.733",
"total_deaths_per_million" : "0.0",
"new_deaths_per_million" : "0.0",
"total_tests" : "",
"new_tests" : "",
"total_tests_per_thousand" : "",
"new_tests_per_thousand" : "",
"new_tests_smoothed" : "",
"new_tests_smoothed_per_thousand" : "",
"tests_units" : "",
"stringency_index" : "0.0",
"population" : "106766.0",
"population_density" : "584.8",
"median_age" : "41.2",
"aged_65_older" : "13.085",
"aged_70_older" : "7.452",
"gdp_per_capita" : "35973.781",
"extreme_poverty" : "",
"cvd_death_rate" : "",
"diabetes_prevalence" : "11.62",
"female_smokers" : "",
"male_smokers" : "",
"handwashing_facilities" : "",
},
.
.
.
},
.
.
.
结构化程序,以便在名为JSONParser的类中使用适当的方法。通过将处理分为读取,写入和/或分析输入数据的单独方法来确保良好的程序结构。
这里是我到目前为止的代码:
public class CSVParser {
private final int ISO_CODE = 0;
private final int DATE = 2;
public CSVParser(File f) throws FileNotFoundException {
try {
Scanner covid_data = new Scanner(f);
String header = covid_data.nextLine();
while(covid_data.hasNextLine()) {
String line = covid_data.nextLine();
String record[] = line.split(",");
String date = record[DATE];
System.out.println(record[ISO_CODE] + ":");
for(String s : record) System.out.println(" "+s);
System.out.println();
}
covid_data.close();
} catch(FileNotFoundException e) {
System.out.println("Problem with file");
}
}
}
这里是主要班级:
import java.io.File;
import java.io.FileNotFoundException;
/*
*
Put the header comment here
*/
public class Main {
public static void main(String[] args) throws FileNotFoundException {
File f = new File("owid-covid-data.csv");
CSVParser csvfile = new CSVParser(f);
}
}