私のコードです:配列インデックスアウト(Java)の
public class countChar {
public static void main(String[] args) {
int i;
String userInput = new String();
userInput = Input.getString("Please enter a sentence");
int[] total = totalChars(userInput.toLowerCase());
for (i = 0; i < total.length; i++);
{
if (total[i] != 0) {
System.out.println("Letter" + (char) ('a' + i) + " count =" + total[i]);
}
}
}
public static int[] totalChars(String userInput) {
int[] total = new int[26];
int i;
for (i = 0; i < userInput.length(); i++) {
if (Character.isLetter(userInput.charAt(i))) {
total[userInput.charAt(i) - 'a']++;
}
}
return total;
}
}
プログラムの目的は、文字列をユーザーに尋ね、その後、各文字が文字列で使用された回数をカウントすることです。
私がプログラムをコンパイルすると、正常に動作します。私はプログラムを実行すると、私はポップアップボックスに文字列を入力することができるが、私は、文字列を提出し、OKを押した後、私は
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26
at countChar.main(countChar.java:14)
は私が何を問題に完全にはわからないと言って、エラーを取得しますそれをどうやって修正するかです。このセミコロンで
私は 'for'ループは' if'文を囲むべきで、最後の ';'は間違っていると思います。 –
14行目の行を指定してください。 ( '// < - ArrayIndexOutOfBoundsException here'を使用してください) – Stewart