在eclipse中编译java程序

时间:2012-10-19 09:48:05

标签: java eclipse compilation

编译此java文件时遇到问题。我无法理解那个问题是什么。 Eclipse说

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at ll.ln.main(ln.java:11)" 

这是目录:

以下是代码:

package ll;
public class ln {
    public static void main(String arguments[]) {
        String[] pharse={"here i am ","ther you are",
                                 "nobody movewho is in charge here",
                                 "haven dosent far away"};
        for (int count=0;count<=pharse.length;count ++){
            String courenttext=pharse[count];
            char[] chcs=courenttext.toCharArray();
            int[] ln=new int[26];
            for(int i=0;i<=chcs.length;i++){
                if((chcs[i]>'z')||(chcs[i]<'a'))
                 continue;
                ln[chcs[i]-'a'] ++;
            }
            for (int i=0;i<27;i++){
                char t='a';
                t+=i;
                System.out.println(t +": "+ln[i]+"   ");
                }
        }
    }
}

4 个答案:

答案 0 :(得分:2)

每个行中的

<=替换为<

for (int count=0;count<=pharse.length;count ++){

应该是:

for (int count=0;count<pharse.length;count ++){

在这一行中也是:

for(int i=0;i<=chcs.length;i++){

应该是:

for(int i=0;i<chcs.length;i++){

答案 1 :(得分:1)

使用<代替<=

for (int i = 0; i < chcs.length; i++) 

完整代码

        String[] pharse = { "here i am ", "ther you are", "nobody movewho is in charge here",
                "haven dosent far away" };
        for (int count = 0; count < pharse.length; count++) {
            String courenttext = pharse[count];
            char[] chcs = courenttext.toCharArray();
            int[] ln = new int[26];
            for (int i = 0; i < chcs.length; i++) {
                if ((chcs[i] > 'z') || (chcs[i] < 'a'))
                    continue;
                ln[chcs[i] - 'a']++;
            }

            for (int i = 0; i < 26; i++) {
                char t = 'a';
                t += i;
                System.out.println(t + ": " + ln[i] + "   ");

            }

        }

答案 2 :(得分:1)

更改

for(int i=0;i<=chcs.length;i++)

for(int i=0;i<chcs.length;i++)

您的索引超过了长度

答案 3 :(得分:0)

更改3行:

  1. for (int count=0;count<=pharse.length;count ++){

    for (int count=0;count<pharse.length;count ++){

  2. for(int i=0;i<=chcs.length;i++){

    for(int i=0;i<chcs.length;i++){

  3. for (int i=0;i<27;i++){

    for (int i=0;i<26;i++){