0

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 i.e. even if it's full).

I'd really appreciate if anyone can see what I am obviously missing!

5
  • Which line is throwing the exception? Commented Apr 15, 2013 at 20:57
  • Are you getting a stack trace with the error? It would be helpful to see where it's throwing the exception. Commented Apr 15, 2013 at 21:02
  • ERROR : java.lang.ArrayIndexOutOfBoundsException: 3 Commented Apr 15, 2013 at 21:16
  • This is all I'm getting so I can't see exactly where it's coming from. Commented Apr 15, 2013 at 21:17
  • Perception, I don't quite understand your comment. Commented Apr 15, 2013 at 21:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, that was meant to be cars[j] not i.
I've changed that but it didn't have any effect.
Add an e.printStackTrace(); to your catch clause. That should narrow it down quickly.
Thank you so much Daniel. Did the stack trace and was able to fix the error. Really appreciate the help!
Not sure how to give you rep but once I figure it out I will send some your way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.