提问人:HarrisonO 提问时间:6/11/2023 更新时间:6/11/2023 访问量:25
扫描程序抛出 java.util.NoSuchElementException:处理大型 CSV 文件时未找到行(750000 行)
Scanner throwing java.util.NoSuchElementException: No line found when processing large CSV file (750000 lines)
问:
您好,我目前正在处理一个大型 Excel 文档,其中包含大约 270 年来世界上每个城市的年温度,并收到错误 java.util.NoSuchElementException:大约 75000 行后找不到行。每行有 8 个元素,这些元素存储在 city 类型的数组列表中,该数组具有以下变量:CountryName、CityName、AvgTemp、MinTemp、MaxTemp、Longitude、Latitude 和 Year。 我用来调试的方法如下:
private static void FillPopulation(){
Connection con = null;
try{
con = DriverManager.getConnection(DATA_BASE);
Scanner lineScanner = new Scanner(new File(EXCEL_TO_PROCESS));
lineScanner.nextLine();
while (lineScanner.hasNext()) {
String line = lineScanner.nextLine();
Scanner rowScanner = new Scanner(line);
rowScanner.useDelimiter(",");
String year = rowScanner.next();
String avgTemp = rowScanner.next();
String minTemp = rowScanner.next();
String maxTemp = rowScanner.next();
String cityName = rowScanner.next();
String countryName = rowScanner.next();
String latitude = rowScanner.next();
String longitude = rowScanner.next();
if (avgTemp.equals("")) {
avgTemp = "0";
}
if (minTemp.equals("")) {
minTemp = "0";
}
if (maxTemp.equals("")) {
maxTemp = "0";
}
String query = "INSERT INTO CityTemps VALUES('" + countryName + "', '" + cityName + "', " + year +", "+avgTemp+", "+maxTemp+", "+minTemp+", '"+latitude+"', '"+longitude+"')" ;
System.out.println(query);
Statement stmt = con.createStatement();
stmt.execute(query);
}
}catch(Exception e){
e.printStackTrace();
}
}
我不确定是什么原因造成的,任何解释都非常感谢。
答: 暂无答案
评论
while (lineScanner.hasNext()) {
while (lineScanner.hasNextLine()) {
1823,2.786,-20.343,23.233,Zhaodong,China,45.81N,125.77E
1823,12.164,3.729,18.618,Zhaotong,China,26.52N,103.88E