java StringIndexOutOfBoundsException:字符串索引超出范围:0

时间:2015-12-13 18:54:08

标签: java arrays string indexing

它的功能非常简单。我不知道为什么它为函数source.charAt(i)提供了StringIndexOutOfBoundsException。我检查了“我”的价值观。不超过字符串的长度,总是为9。

public static String getClockResetString(String source, String target, Hashtable order)
{
    String temp = "",name;

    for(int i = 0; i < source.length(); i++){
        name = (String)order.get(""+i);

                if(source.charAt(i) != target.charAt(i))
                {
                    if((int)source.charAt(i) < (int)target.charAt(i)){
                        temp = "h" + name + "=" + "dp" + name + "0" + " do " + "{h" + name + "'=0, k'=k+1} ";
                    }
                    else{
                        temp = "h" + name + "=" + "dn" + name + "1" + " do " + "{h" + name + "'=0, k'=k+1} ";
                    }
                }

    }

    return temp;
}

1 个答案:

答案 0 :(得分:0)

我打赌你超出了目标界限,而不是来源

if(source.charAt(i) != target.charAt(i))

您需要检查

i < target.length()