所以我有这段代码,我在从csv文件中检索数据并将它们放入数组时遇到了麻烦。

这就是我的CSV文件中的内容

D001,55,Lab,Butch
D002,22,Husky,Ben
D003,12,Maltese,John
D004,34,GermanSheperd,James
D005,76,Rot,Smith
public static void CSVInputFile() throws IOException {

    FileReader inFileReader;
    BufferedReader in;
    String inStr;
    File myFile;
    String dogID;
    int size;
    String breed;
    String name;

    myFile = new File("DogFile.csv");
    inFileReader = new FileReader(myFile);
    in = new BufferedReader(inFileReader);

    inStr = in.readLine();

    Dog[] NewReadDog = new Dog[5];

    int i = 0;
    while (inStr != null) {
        StringTokenizer dogTok = new StringTokenizer(inStr, ",");

        while (dogTok.hasMoreTokens()) {

            dogID = dogTok.nextToken();
            size = new Integer(dogTok.nextToken());
            breed = dogTok.nextToken();
            name = dogTok.nextToken();
            NewReadDog[i] = new Dog(dogID, size, breed, name);
            i++;
            System.out.println("dog " + i + " is stored");
        }
    }

    System.out.println("\nOutput Dogs from CSV FILE: ");

    for (int count = 0; count < NewReadDog.length; count++) {
        System.out.println(NewReadDog[count]);

    }

    in.close();

}


我刚刚开始学习编码,所以请多多包涵。
谢谢

最佳答案

完成对当前行进行标记化后,您必须阅读下一行:

while (inStr != null) {
    StringTokenizer dogTok = new StringTokenizer(inStr, ",");

    while (dogTok.hasMoreTokens()) {
        [...]
    }
    System.out.println("dog " + i + " is stored");
    inStr = in.readLine();
    i++; //replaced here
}

07-26 02:24