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

}
  • 1
    Arrays are 0-indexed in Java, so a size 10 array has legal indexes of 0-9. 10 is out of bounds. – azurefrog Dec 16 '14 at 22:09
  • its works in other places in my code though like if i put System.out.println(info[10]); it will work – MatthewLogique Dec 16 '14 at 22:10
  • 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. – Alexis C. Dec 16 '14 at 22:10
  • How many elements does `info` have after splitting? – August Dec 16 '14 at 22:10
  • `info = line.split(",");` is throwing away the `String[20]` and pointing to a new array created by the `split()` method, which may be shorter. – azurefrog Dec 16 '14 at 22:11
  • it has 11 elements after splitting – MatthewLogique Dec 16 '14 at 22:11
  • 1
    @MatthewLogique That's not possible, otherwise you wouldn't have this exception. – Alexis C. Dec 16 '14 at 22:12
  • Obviously it doesn't have 11 since 10 is out of bounds. – azurefrog Dec 16 '14 at 22:12
  • If it has 11 elements after splitting, then its size is not 10. :/. – Zéychin Dec 16 '14 at 22:12
  • Step through it with a debugger. See what its size REALLY is, instead of guessing. – Dawood ibn Kareem Dec 16 '14 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. – azurefrog Dec 16 '14 at 22:13
  • possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Semih Eker Dec 16 '14 at 22:14
  • yep I agree .. do something like this: if (info.length > 10 && info[10].toLowerCase()... ) – LeTex Dec 16 '14 at 22:14
  • 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. – Dawood ibn Kareem Dec 16 '14 at 22:16
  • @DavidWallace - true. but it seems to me that info = line.split(","); is dynamic. you will never know the value unless you are reading static/same file every time. So if he want to check the String at index 10 for some reason, I am afraid that is a good way to go. – LeTex Dec 16 '14 at 22:19
  • @LeTex No, he needs to understand why `split` is not giving him the result he's expecting. I believe it's probably to do with how `split` behaves on empty fields (that is, two consecutive commas, or whatever other delimiter you're using). He needs to do his own detective work to work out what the cause of the problem is, and then fix it. The "head in the sand" approach to software development that you are advocating will bite you in the long run. I believe you have given him very bad advice. – Dawood ibn Kareem Dec 16 '14 at 22:21
  • @DavidWallace - I agree with you man. in the first place I didn't like the logic of putting hard coded magic number as index of the array. However if that is so, the question is a String at index 10 is always expected to be present ? or as you suggested is the split of the string working as expected? - either way it has to be studied properly! I agree! – LeTex Dec 16 '14 at 22:26

1 Answers1

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 =).
   // ...
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249