java - 当我的 CSV 包含 5 列时,为什么我的变量 lat 返回数组越界?
问题描述
我尝试使用 Java 处理 CVS 文件。我完成了前 3 列所需的操作,但 dataContent[3] 返回错误。我不明白为什么我的CSV 文件在这里可用,是 5 列。任何想法 ??
while ((lsLigne = br.readLine()) != null) {
dataContent = lsLigne.split(";");
for (i = 0; i < dataContent.length; i++) {
cities = dataContent[0];
insee = dataContent[1];
lng = dataContent[2];
lat = dataContent[3]; // line causing problems
}
if (cities.isEmpty()) {
System.out.println("Attention nom de commune manquant");
} else if (!cities.matches(motifLettersOnly)) {
System.out.println("Attention nom de ville erroné : " + cities);
} else if (insee.isEmpty()){
System.out.println("Attention l'Insee est manquant");
} else if(!insee.matches(motifNumbersOnly)){
System.out.println("Attention Insee erronée : " + insee);
} else if(lng.isEmpty()){
System.out.println("Attention la longitude est manquante");
} else if(!lng.matches(motifNumbersOnly)){
System.out.println("Attention longitude erronée " + lng);
}
}
nom;insee;lng;lat;
OZAN;1284;4.91667;46.3833;
CORMORANCHE-SUR-SAONE;1123;4.83333;46.2333;
PLAGNE;1298;5.73333;46.1833;
TOSSIAT;1422;5.31667;46.1333;
POUILLAT;1309;5.43333;46.3333;
TORCIEU;;5.4;45.9167;
REPLONGES;1320;4.88333;Hello;
11111;1119;5.58333;46.0333;Champ sup
PERON;1288;5.93333;46.2;
RELEVANT;Hello;4.95;46.0833;
CHAVEYRIAT;1096;5.06667;46.1833;
;1431;5.35;45.9167;
MAILLAT;1228;;46.1333;
FARAMANS;1156;5.11667;;
BEON;1039;5.75;45.8333;
SAINT-BERNARD;1339;4.73723;lol;
ROSSILLON;1329;zaz;45.8333;
解决方案
这个循环
for (i = 0; i < dataContent.length; i++)
完全没用,你可以摆脱它。
我查看了您的 CSV,您在 Faramas 列中对 lat 没有任何价值,这可能就是您遇到越界异常的原因,因为您正在遍历每一行
while ((lsLigne = br.readLine()) != null)
您可以像这样避免越界异常,如果值在 csv 中不可用,则将其留空。
while ((lsLigne = br.readLine()) != null) {
dataContent = lsLigne.split(";");
if (dataContent.length > 0) {
cities = dataContent[0];
if (dataContent.length > 1)
insee = dataContent[1];
if (dataContent.length > 2)
lng = dataContent[2];
if (dataContent.length > 3)
lat = dataContent[3]; // line no longer causing problems
if (cities == null || cities.isEmpty()) {
System.out.println("Attention nom de commune manquant");
} else if (!cities.matches(motifLettersOnly)) {
System.out.println("Attention nom de ville erroné : " + cities);
} else if (insee == null || insee.isEmpty()){
System.out.println("Attention l'Insee est manquant");
} else if(!insee.matches(motifNumbersOnly)){
System.out.println("Attention Insee erronée : " + insee);
} else if(lng == null || lng.isEmpty()){
System.out.println("Attention la longitude est manquante");
} else if(!lng.matches(motifNumbersOnly)){
System.out.println("Attention longitude erronée " + lng);
}
}
}
当然也有更好的方法来处理这个逻辑,所以你可以从这里得到它。
如果你真的想使用那个 for 循环,你也可以这样做。
for (int i = 0; i < dataContent.length; i++) {
switch(i) {
case 0: cities = dataContent[i]; break;
case 1: insee = dataContent[i]; break;
case 2: lng = dataContent[i]; break;
case 3: lat = dataContent[i]; break;
}
}
推荐阅读
- javascript - 在 zeit-now 上通过 express 应用程序使用链接到 html 的 CSS 和 JS 客户端文件
- github-api - 您如何通过 github 应用从上游存储库获取通知?
- android - 使用加速度计了解手机的运动速度并做出反应(Android Studio)
- sql - 使用 uniq ID 对日期范围和岛屿进行分组,而无需按分隔符进行分区
- documentation - 我想做文档查看器
- mysql - 如何根据 R Mysql DB 中的时间戳删除表?
- android - 无法使用 Retrofit 2.6.1 成功发送 POST 请求 - JSON 转换器的问题
- node.js - 如何从谷歌云应用引擎服务获取服务器端网络统计信息(如 Webalizer)
- dataframe - 删除火花数据框中的标点符号
- php - 从数据库中获取数据并通过 phpMailer 发送到邮件