7

Here is my code:

public class countChar {

    public static void main(String[] args) {
        int i;
        String userInput = new String();

        userInput = Input.getString("Please enter a sentence");

        int[] total = totalChars(userInput.toLowerCase());

        for (i = 0; i < total.length; i++);
        {
            if (total[i] != 0) {
                System.out.println("Letter" + (char) ('a' + i) + " count =" + total[i]);
            }
        }
    }

    public static int[] totalChars(String userInput) {
        int[] total = new int[26];
        int i;
        for (i = 0; i < userInput.length(); i++) {
            if (Character.isLetter(userInput.charAt(i))) {
                total[userInput.charAt(i) - 'a']++;
            }
        }
        return total;
    }
}

The program's purpose is to ask the user for a string, and then count the number of times each character is used in the string.

When I go to compile the program, it works fine. When I run the program, I am able to enter a string in the popup box, but after I submit the string and press OK, I get an error, saying

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26
at countChar.main(countChar.java:14)

I'm not completely sure what the problem is or how to fix it.

2
  • 7
    I think the for loop should enclose the if statement and the ; at the end is wrong. Commented Aug 31, 2013 at 15:51
  • Could you please indicate which line is line 14? (Use // <-- ArrayIndexOutOfBoundsException here) Commented Aug 31, 2013 at 15:52

4 Answers 4

18
for ( i = 0; i < total.length; i++ );
                                    ^-- remove the semi-colon here

With this semi-colon, the loop loops until i == total.length, doing nothing, and then what you thought was the body of the loop is executed.

Sign up to request clarification or add additional context in comments.

3 Comments

I feel a different formatting of the { (ie on the end of the for line) could have made this obvious.
I updated the code, and I now get this error: countChar.java:18: error: illegal start of expression public static int[]totalChars( String userInput ) ^ That error occurs for all of the other words on the second method declaration line.
for (i = 0; !compiler.someoneFixesTheCompilerToWarnAboutThisError(); i++) StackOverflow.answerQuestionBySomeoneToMakeThisSameErrorAgain(); What is the current value of i? I suspect it's in the thousands by now ...
9
for ( i = 0; i < total.length; i++ ); // remove this
{
    if (total[i]!=0)
        System.out.println( "Letter" + (char)( 'a' + i) + " count =" + total[i]);
}

The for loop loops until i=26 (where 26 is total.length) and then your if is executed, going over the bounds of the array. Remove the ; at the end of the for loop.

Comments

1

This is Very Good Example of minus Length of an array in java, i am giving here both examples

 public static int linearSearchArray(){

   int[] arrayOFInt = {1,7,5,55,89,1,214,78,2,0,8,2,3,4,7};
   int key = 7;
   int i = 0;
   int count = 0;
   for ( i = 0; i< arrayOFInt.length; i++){
        if ( arrayOFInt[i]  == key ){
         System.out.println("Key Found in arrayOFInt = " + arrayOFInt[i] );
         count ++;
        }
   }

   System.out.println("this Element found the ("+ count +") number of Times");
return i;  
}

this above i < arrayOFInt.length; not need to minus one by length of array; but if you i <= arrayOFInt.length -1; is necessary other wise arrayOutOfIndexException Occur, hope this will help you.

Comments

-2
import java.io.*;
import java.util.Scanner;
class ar1 {
    public static void main(String[] args) {
        //Scanner sc=new Scanner(System.in);
        int[] a={10,20,30,40,12,32};
        int bi=0,sm=0;
        //bi=sc.nextInt();
        //sm=sc.nextInt();
        for(int i=0;i<=a.length-1;i++) {
            if(a[i]>a[i+1]) 
                bi=a[i];

            if(a[i]<a[i+1])
                sm=a[i];
        }
        System.out.println("big"+bi+"small"+sm);
    }
}

1 Comment

Instead of simply posting a block of code, please explain your answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.