0

I'm getting a "java.lang.ArrayIndexOutOfBoundsException:10" error on my "if" statement even though the file im getting the data from has an index enough to be 10.

When I call info[10] in other places in my code it works but i dont know why it's not working here.

learning file management unit in computer science right now...

public static void comSci(String onTheMap) throws IOException  
{         
    BufferedReader input = new BufferedReader (new FileReader ("data.txt"));
    if (onTheMap.equals("3")){    
        Scanner scanner = new Scanner("data.txt");
        String line="x";
        System.out.println("--------------------------------------------------------------");
        System.out.println("Create a file of student's who are enrolled in ICS3U0:");
        System.out.println("--------------------------------------------------------------");
        String info[] = new String[20];
        boolean finder = false;      

        while (line!=null) {
            line = input.readLine();
            if (line==null)
                break;        
            info = line.split(",");

            if (info[10].toLowerCase().contains("ICS3U0".toLowerCase())) {  //PROBLEM
                finder = true;
                String programmers = info[0] + "," + info[1];
                System.out.println(programmers);
                try {
                    FileWriter theFile = new FileWriter("ICS3U0.txt",true);
                    PrintWriter names = new PrintWriter(theFile);
                    names.println();
                    names.close();

                }
                catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }  
        } 
        System.out.println("ICS3U0.TXT WAS CREATED");

    }
    input.close();

}
17
  • 1
    Arrays are 0-indexed in Java, so a size 10 array has legal indexes of 0-9. 10 is out of bounds. Commented Dec 16, 2014 at 22:09
  • 2
    Check the length of line.split(",");. If you are sure that there should be at least 11 elements, then there's something wrong with your input data. Commented Dec 16, 2014 at 22:10
  • 1
    @MatthewLogique That's not possible, otherwise you wouldn't have this exception. Commented Dec 16, 2014 at 22:12
  • 2
    Check for info.length > 10 before you try to access info[10]. I guarantee the array is smaller than you think. Also, you should update the title of your question, it is misleading. Commented Dec 16, 2014 at 22:13
  • 1
    @LeTex No, don't do that. That would just hide whatever bug is causing it to be less than 10. He needs to find out what's happening, rather than sticky-taping over the problem. This is what a debugger is for. Commented Dec 16, 2014 at 22:16

1 Answer 1

2

Java array indexes start at 0. So an array of length 10 has valid indexes from 0-9 inclusive (or 0-10 exclusive). This is why the for loop is usually styled,

for (int i = 0; i < arr.length; i++) { // <-- less than (not less than =).
   // ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

That wasn't the problem
Yes it was. The fact that LeTex's advice worked for you PROVES that it was.

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.