Java 循环和字段的indexoutofboundsexception

Java 循环和字段的indexoutofboundsexception,java,eclipse,string,indexoutofboundsexception,Java,Eclipse,String,Indexoutofboundsexception,显示错误消息: 线程“main”java.lang.StringIndexOutOfBoundsException中出现异常:字符串索引超出范围:42 位于java.lang.String.charAt(未知源) 在CopyOfAufgabe_2.main(CopyOfAufgabe_2.java:13)中,`for(int ef=0;ef感谢您的回答。很抱歉误解。数组应该有26个元素(因为英文字母表中有26个字母),我把数组的设置搞错了。刚刚纠正了它…但是,它仍然不起作用。问题完全相同。@Aa

显示错误消息:

线程“main”java.lang.StringIndexOutOfBoundsException中出现异常:字符串索引超出范围:42 位于java.lang.String.charAt(未知源)
在CopyOfAufgabe_2.main(CopyOfAufgabe_2.java:13)中,`

for(int ef=0;ef感谢您的回答。很抱歉误解。数组应该有26个元素(因为英文字母表中有26个字母),我把数组的设置搞错了。刚刚纠正了它…但是,它仍然不起作用。问题完全相同。@Aabbccc我已经测试了你的代码。如果你解决了上面提到的两个问题,它就会起作用。@Aabbccc你解决了第二个问题了吗?(
ab@Eran谢谢你的第二个建议。它现在起作用了。
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.)