【发布时间】:2017-12-31 19:07:59
【问题描述】:
class Main{
public static void main (String str[]) throws IOException{
Scanner scan = new Scanner (System.in);
String message = scan.nextLine();
String[] sWords = {" qey ", " $ "," ^^ "};
int lenOfArray = sWords.length;
int c = 0;
int[] count = {0,0,0};
在 for 循环之一中出现错误 "java.lang.StringIndexOutOfBoundsException: String index out of range: -1" 。我希望程序检查 sWord 数组中的每个子字符串,并计算它在主消息输入中出现的次数。
for (int x = 0; x < sWords.length; x++){
for (int i = 0, j = i + sWords[x].length(); j < message.length(); i++){
if ((message.substring(i,j)).equals(sWords[x])){
count[c]++;
}
}
}
}
}
【问题讨论】:
-
在您的循环中,i 的值高于 j 和消息字符串的大小,因此您得到 -1
标签: java for-loop indexoutofboundsexception