Counting the number of occurrences of characters in an array

Posted by Anthony Pittelli on Stack Overflow See other posts from Stack Overflow or by Anthony Pittelli
Published on 2012-11-18T23:42:03Z Indexed on 2012/11/19 5:00 UTC
Read the original article Hit count: 163

Filed under:
|
|

This is what I have but it is not working, this is confusing for me. If you scroll down I commented on someones post the exact problem I am having and what I am trying to do. I was thinking maybe the problem is my code to generate the random characters:

public void add (char fromChar, char toChar){
    Random r = new Random(); //creates a random object
    int randInt;
    for (int i=0; i<charArray.length; i++){
        randInt = r.nextInt((toChar-fromChar) +1);
        charArray[i] = (char) randInt; //casts these integers as characters
    }
}//end add
public int[] countLetters() {
int[] count = new int[26];
char current;

     for (int b = 0; b <= 26; b++) {
         for (int i = 97; i <= 123; i++) {
             char a = (char) i;
             for (int ch = 0; ch < charArray.length; ch++) {
                 current = charArray[ch];
                if (current == a) {
                count[b]++;
                }
             }
         }
     }
return count;
}

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays