首页 > 解决方案 > 当我的 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;

标签: java

解决方案


这个循环

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;
    }
}

推荐阅读