gpt4 book ai didi

java - 是否有 CSV 扫描未找到任何行 in.nextLine();

转载 作者:行者123 更新时间:2023-12-02 05:52:27 26 4
gpt4 key购买 nike

我必须使用 CSV 文件,但我不需要第一行,我尝试过使用: be.nextLine() 它适用于其他项目,但不适用于此项目。 Scanner be = new Scanner(new File("autok.csv"))

我使用的 CSV 文件如下所示:

第一行: |出发 |目的地 |数量 |电话 |房间|第二行:|布达佩斯 |莫斯科 | PQA-209| 30/5555555 | 30/5555555 5 |第三线|布达佩斯 |柏林 | ASD-444| 30/4444855 | 30/4444855 3 |

这是我尝试过的,并且与其他项目完美配合:

static ArrayList<autok> kocsi = new ArrayList<>();

public static void main(String[] args) {
try (Scanner be = new Scanner(new File("autok.csv"))){
be.nextLine();
while(be.hasNextLine()){
String[] s = be.nextLine().split(";");
autok d;
d = new autok(s[0],s[1],s[2],
Integer.parseInt(s[3]),
Integer.parseInt(s[4]));
kocsi.add(d);
}
}catch (IOException ex) {
System.out.println("Hiba");
}

int asd = kocsi.size();
for(int i =0; i<asd;i++){
System.out.println("hirdetők száma: " + kocsi.size());
}

}```

POJO:

        this.indulas = indulas;
this.cel = cel;
this.rendszam = rendszam;
this.telefonszam = telefonszam;
this.ferohely = ferohely;
}
private String indulas;
private String cel;
private String rendszam;
private int telefonszam;
private int ferohely;

还有GETTERS。

看起来我想使用这个 CSV,但它返回了错误: java.util.NoSuchElementException:找不到以下行:be.nextLine();任何帮助将不胜感激。

最佳答案

工作正常be.nextLine();但是你的代码需要一些升级

static ArrayList<autok> kocsi = new ArrayList<>();

public static void main(String[] args) {
try (Scanner be = new Scanner(new File("src/com/test/autok.csv"))) {
be.nextLine();
while (be.hasNextLine()) {
String[] s = be.nextLine().split("\\|");

autok d;
d = new autok(s[1], s[2], s[3], s[4], Integer.parseInt(s[5].trim()));
kocsi.add(d);
}
} catch (IOException ex) {
System.out.println("Hiba");
}

int asd = kocsi.size();
for (int i = 0; i < asd; i++) {
System.out.println("hirdetk szma: " + kocsi.size());
}

}

Autok 类

public class Autok {

private String indulas;
private String cel;
private String rendszam;
private String telefonszam;
private int ferohely;

public Autok(String indulas, String cel, String rendszam, String s, int ferohely) {
this.indulas = indulas;
this.cel = cel;
this.rendszam = rendszam;
this.telefonszam = s;
this.ferohely = ferohely;
}

// getter & setter
}

CSV 文件

| Departure | Destination | Number | Tel | room |
| Budapest | Moscow | PQA-209| 30/5555555 | 5 |
| Budapest | Berlin | ASD-444| 30/4444855 | 3 |

关于java - 是否有 CSV 扫描未找到任何行 in.nextLine();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56038294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com