简体   繁体   English

Java读取.csv文件并获取java.lang.ArrayIndexOutOfBoundsException:3,不知道为什么

[英]Java Reading .csv file and getting java.lang.ArrayIndexOutOfBoundsException: 3 and don't know why

I'm banging my head against a wall about a program I'm trying to complete. 我正在为要完成的程序撞墙。 I'm sure the answer is simple but I just can't figure out the solution. 我敢肯定答案很简单,但我只是想不出解决办法。

When I write to the csv file it works but when reading from it, if there is more than 3 objects in the csv file, I get the ArrayIndex error but three or less and it throws no error. 当我写入csv文件时,它可以工作,但是当从其中读取时,如果csv文件中有3个以上的对象,则会收到ArrayIndex错误,但三个或更少,并且不会引发任何错误。

Below is my code for writing to the file: 以下是我写入文件的代码:

void saveDataToFile() { void saveDataToFile(){

    String op = "";

    try {
        FileWriter fw = new FileWriter(filename);
        for (int i =0 ; i< library.length ; i++)
            if(library[i]!=null)
            fw.write(library[i].getDetailsCSV().toString()+"\n");
        fw.write(op.toString());
        fw.close();
    }
    catch(Exception e) {
        System.out.println("ERROR : "+e);
    }
    System.out.println("saveDataToFile()");

}

Below is the code for reading the file: 下面是读取文件的代码:

void loadDataFromFile() { void loadDataFromFile(){

    try{
        File fi = new File(filename);
        FileReader fr = new FileReader(fi);
        char[] buffer = new char[(int)fi.length()];
        fr.read(buffer);
        fr.close();

        String all = new String(buffer);
        String[] ip = all.split("\n");

        for (int i=0; i<ip.length; i++){ 
            String[] op = ip[i].split(",");

            String author = op[0];
            String title = op[1];
            int isbn = Integer.parseInt(op[2]);
            String s = op[3];
            boolean h = Boolean.parseBoolean(op[3]);

            for(int j=0; j<op.length; j++){
                if(author.equals("Dickens"))
                    library[i] = new title(author,isbn);
                else if(author.equals("Lumas"))
                    library[i] = new title(author,isbn,s);
                else if(author.equals("Orwell"))
                    library[i] = new title(author,isbn,h);

            }
        }

    }
    catch(Exception e) {
        System.out.println("ERROR : "+e);
    }
            System.out.println("loadDataFromFile()");
    }

The library[] array is size 10. I've tried a System.out.println(op.length); library []数组的大小为10。我已经尝试过System.out.println(op.length);。 and System.out.println(ip.length); 和System.out.println(ip.length); from the read method and ip.length is 10 and op.length is 3 (regardless of how many objects have actually been saved to the csv file ie even if it's full). 从读取方法中,ip.length为10,op.length为3(无论实际上已将多少对象保存到csv文件中,即使已满)。

I'd really appreciate if anyone can see what I am obviously missing! 如果有人能看到我显然想念的东西,我将不胜感激!

I'm guessing this loop is breaking it: 我猜想这个循环正在打破它:

       for(int j=0; j<op.length; j++){
            if(make.equals("Ford"))
                cars[i] = new Ford(model,year);
            else if(make.equals("Mazda"))
                cars[i] = new Mazda(model,year,colour);
            else if(make.equals("Toyota"))
                cars[i] = new Toyota(model,year,h);

        }

If you have more than 3 lines this loop will fail since you are using i instead of j. 如果您有3行以上,则此循环将失败,因为您使用的是i而不是j。 Not sure what this loop is trying to do but that part will fail for sure. 不确定此循环将要执行的操作,但是该部分肯定会失败。

Also, if op.length is 3, index 3 won't be there since Java arrays are indexed from 0 not 1. 另外,如果op.length为3,则索引3将不存在,因为Java数组是从0而不是1索引的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM