public class Task {
public static void main(String[] args) {
String text ="a subject is the noun that is doing the main verb.";
int[] field = new int[26];
int xy = 0; //xy will be the total number of the letter in the sentence
char cd = 'a';
for (int ef = 0; ef<= 25; ef++){
for (int ab = 1; ab<= text.length(); ab++){
if (cd == text.charAt(ab)){ // Eclipse says that this line has problem (at CopyOfAufgabe_2.main(CopyOfAufgabe_2.java:13)
field[ef]=field[ef]+1;
xy++;
}
}
cd++;
}
}
}
1. find the total number of letters in the sentence
2. find the number of respective letter (i.e. how many "a"s, how many "b"s, etc. The numbers are assigned to the field created in the fifth line (one number for each object)) (It is assumed that every letter in the sentence is not capitalized.)
显示错误消息:
线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:42 在java.lang.String.charAt(未知来源) 在CopyOfAufgabe_2.main(CopyOfAufgabe_2.java:13)``
答案 0 :(得分:2)
for (int ef = 0; ef<= 25; ef++){
应该是
for (int ef = 0; ef< 25; ef++){
包含25个元素的数组的索引从0到24。
for (int ab = 1; ab<= text.length(); ab++){
应该是
for (int ab = 0; ab< text.length(); ab++){
具有length()
个字符的字符串的字符具有从0到length()-1
的索引。