2

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]+"   ");
                }
        }
    }
}
1
  • You are mismatching the String and .toCharArray(); what you want to do? Commented Oct 19, 2012 at 9:50

4 Answers 4

2

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++){
Sign up to request clarification or add additional context in comments.

Comments

1

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

            }

        }

Comments

1

Change

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

To

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

Your index exceeds the length

Comments

0

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++){

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.