私は、文字列の集合の平均ワード長を取得しようとしていますし、私は以下のエラーに実行しています:範囲外の文字列インデックス:平均ワード長
java.lang.StringIndexOutOfBoundsException :-1
私は原因がわかりません。
がstatic double getAverageWordLength() {
// TODO: update the code here to return the average length of words in the tweets processed so far.
String[] average = tweeps.split(" ");
int count = 0;
int sum = 0;
int currentWordLength;
String tempString;
for(int i = 0; i < average.length; i++)
{
average[i] = average[i].replaceAll("[^a-zA-Z]", "").toLowerCase();
tempString = average[i];
currentWordLength = tempString.length();
sum += currentWordLength;
count++;
}
double averageWordLength = sum/count;
return averageWordLength;
}
コードの行は、例外から来るん:ここに私のコードはありますか? – 4castle
私の環境では再生できません。 'tweeps'の価値は何ですか? – ymonad
ちなみに、 'sum/count'は整数除算をしています(小数点以下の数字は切り捨てます)。操作を実行する前に 'double * 'に計算を進めるために、おそらく' 1.0 * sum/count'が必要です。 – 4castle