max
max

Reputation: 6177

compile java program in eclipse

I have problem compiling this java file. I cant understand what that problem is. Eclipse says

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

here is the directory:

Here is the code:

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]+"   ");
                }
        }
    }
}

Upvotes: 2

Views: 567

Answers (4)

RAS
RAS

Reputation: 8158

Change 3 lines:

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

    to

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

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

    to

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

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

    to

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

Upvotes: 0

xfx
xfx

Reputation: 1948

in each of these lines replace <= with < :

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

should be:

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

in this line also :

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

should be :

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

Upvotes: 2

madhairsilence
madhairsilence

Reputation: 3870

Change

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

To

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

Your index exceeds the length

Upvotes: 1

Drogba
Drogba

Reputation: 4346

Use < instead of <=

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

Complete code

        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] + "   ");

            }

        }

Upvotes: 1

Related Questions