【问题标题】:java StringIndexOutOfBoundsException: String index out of range: 0java StringIndexOutOfBoundsException:字符串索引超出范围:0
【发布时间】:2015-12-13 18:54:07
【问题描述】:

这是一个非常简单的功能。我不知道为什么它会为函数 source.charAt(i) 提供 StringIndexOutOfBoundsException。我检查了 'i' 的值不超过字符串的长度,该长度始终为 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;
}

【问题讨论】:

  • 你传递给这个方法的参数是什么? source 为空。
  • 例外不说谎。
  • 可能targetsource 短。
  • 鉴于例外情况,您的字符串中的一个似乎是空字符串...另外,呃,为什么在 2015 年使用 Hashtable?它已经过时了至少10年......
  • 它是一个 9 位的基因序列。这是我主管的旧代码。源和目标不为空。订单也在起作用。少数州出现异常。

标签: java arrays string indexing


【解决方案1】:

我敢打赌,您超出了目标范围,而不是来源范围

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

你需要检查一下

i < target.length()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多