java.lang.ArrayIndexOutOfBoundsException:

时间:2015-07-13 06:46:19

标签: java string

我有一个<div> <a href="#" target="_blank"><img src="#" class="btn" /></a> </div> .btn { float: right; bottom:0; margin-right:-12px; margin-bottom:-6px; position:absolute; right:0; } ,如果我在String = "abc model 123 abcd1862893007509396 abcd2862893007509404"abcd1之间提供空格number例如。 abcd1 862893007509396我的代码可以正常使用,但如果没有像abcd1862893007509396这样的空格,我会得到java.lang.ArrayIndexOutOfBoundsException,请帮帮忙?:

PFB代码:

String text = "";
final String suppliedKeyword = "abc model 123 abcd1862893007509396 abcd2862893007509404";
String[] keywordarray = null;
String[] keywordarray2 = null;
String modelname = "";
String[] strIMEI = null;

if ( StringUtils.containsIgnoreCase( suppliedKeyword,"model")) {
    keywordarray = suppliedKeyword.split("(?i)model");

    if (StringUtils.containsIgnoreCase(keywordarray[1], "abcd")) {
        keywordarray2 = keywordarray[1].split("(?i)abcd");
        modelname = keywordarray2[0].trim(); 

        if (keywordarray[1].trim().contains(" ")) {
            strIMEI = keywordarray[1].split(" ");
            for (int i = 0; i < strIMEI.length; i++) {
                if (StringUtils.containsIgnoreCase(strIMEI[i],"abcd")) {
                    text = text + " " + strIMEI[i] + " "
                            + strIMEI[i + 1];
                    System.out.println(text);
                }
            }
        } else {
            text = keywordarray2[1];            
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在查看您的代码后,我唯一可以考虑的错误原因是

if (StringUtils.containsIgnoreCase(strIMEI[i],"abcd")) {
    text = text + " " + strIMEI[i] + " "
    + strIMEI[i + 1];
    System.out.println(text);
}

您正在尝试访问strIMEI [i + 1],如果您在strIMEI中的最后一个元素包含&#34; abcd&#34;

,则会抛出错误