0

I am writing this class for another program and I keep getting an Array out of bounds exception when I am splitting the string str.

    static String[][] readFile(){
    BufferedReader in;
    count = 0;
    String[][] ret = new String[20][2];
    try
    {
        in = new BufferedReader(new FileReader(CFGfolder+"\\input.ini"));
        String str;

        while((str=in.readLine()) != null){
            if(str!="[GameEvents]"){
                ret[count][0]=str.split("=")[0];
                ret[count][1]=str.split("=")[1];
                count++;
            }
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return ret;

}

This is the format of the file it is reading.

evtCastSpell1=[q]
evtCastSpell2=[w]
evtCastSpell3=[e]
evtCastSpell4=[r]

I tried seeing what the str string really is (meaning, print it without the splitting) and it shows that it is correctly reading the file and putting it in the array. The problem comes when I try to read the [1] of the splitted string. The [0] is fine and it correctly prints

evtCastSpell1
evtCastSpell2
evtCastSpell3
evtCastSpell4

I really have no idea why it is not reading the [1] part. Usually this kinds of errors come from regex expressions, is it understanding the [ in the second part as a regex expression? How would I get around that if so? I've tried looking around but all I can find is similar errors that happen because of regex expressions. Is this one the same?

Thank you for your help.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Nacht
  • 10,488
  • 8
  • 31
  • 39
  • not what you asked but you should use `new FileReader(new File(CFGfolder, "input.ini"))` to avoid platform specific coding. – MeBigFatGuy Sep 22 '11 at 06:06
  • you also could probably do `ret[count]=str.split("=");` to avoid doing split twice. – MeBigFatGuy Sep 22 '11 at 06:08
  • While the reason of the exception, and correct code is given in sudmong's answer, Your code can have other problems too. If count exceeds the array size 20, then there will also be an arrayindexoutofboundexception. –  Sep 22 '11 at 06:09

2 Answers2

4

The reason for the exception could be the number of lines in file going more than the size of your array. Also replace this if(str!="[GameEvents]") with if(!str.equals("[GameEvents]")) for getting correct results.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
sudmong
  • 2,036
  • 13
  • 12
  • I don't see how that's the problem. The issue is that the array str.split("=")[1] doesn't exist. The issue is that the array being created by the split is length 1, so only the [0] element exists. So the question is, why is that? – Nacht Sep 22 '11 at 06:26
  • See my answer, reason for exception could be "could be the number of lines in file going more than the size of your array" . And for If condition see @The Elite Gentleman's answer – sudmong Sep 22 '11 at 07:15
2

You are doing a reference equality comparison instead of a string equals() comparison:

I would do this:

while((str=in.readLine()) != null){
    if(!"[GameEvents]".equals(str)) {
        String[] splits = str.split("=");
        ret[count][0]=splits[0];
        ret[count][1]=splits[1];
        count++;
    }
}

The reason of you ArrayIndexOutOfBoundsException is because of this:

if(str!="[GameEvents]"){

Because of the reference equality, the if expression returns a true and you're doing a split() on the value [GameEvents].

I hope this helps:

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228