简体   繁体   English

使用String.split时,我不断收到“ ArrayIndexOutOfBounds”异常

[英]I keep getting a “ArrayIndexOutOfBounds” Exception when using String.split

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 . 我正在为另一个程序编写此类,并且在拆分字符串str时,总是出现Array out of bounds异常。

    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. 我试着看一下str字符串的真正含义(意思是不分割就打印它),它表明它正确地读取了文件并将其放入数组中。 The problem comes when I try to read the [1] of the splitted string. 当我尝试读取拆分后的字符串的[1]时,问题就来了。 The [0] is fine and it correctly prints [0]很好,可以正确打印

evtCastSpell1
evtCastSpell2
evtCastSpell3
evtCastSpell4

I really have no idea why it is not reading the [1] part. 我真的不知道为什么它不阅读[1]部分。 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. 谢谢您的帮助。

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. 还要用if(!str.equals("[GameEvents]"))替换此if(str!="[GameEvents]") if(!str.equals("[GameEvents]"))以获得正确的结果。

You are doing a reference equality comparison instead of a string equals() comparison: 您正在执行引用相等比较,而不是字符串equals()比较:

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: 出现ArrayIndexOutOfBoundsException的原因是:

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

Because of the reference equality, the if expression returns a true and you're doing a split() on the value [GameEvents] . 由于引用相等,因此if表达式返回true并且您正在对[GameEvents]值进行split()

I hope this helps: 我希望这有帮助:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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