简体   繁体   中英

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.

Below is my code for writing to the file:

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() {

    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); and 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).

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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