Character Occurence

I am new to java and I have an assignment that asked me to count the number of character and the number of occurrence. I have figured out the character count be I am not sure how to figure the number of occurrence. Can someone point me in the right direction? Here is my code so far..

Re: Character Occurence

Re: Character Occurence

Are you trying to count how many times each character occurs?

In that case you could use a Map<Character,Integer>. The key of a map entry would be the character you found as line.charAt(i) and its value would be the count. Each time you find a character you would increment the count (or, if it there is no map entry for that character yet you would create one with a count of 1).

Re: Character Occurence

You don't read the letter into the array. You use the array to store the totals for each letter. Total for 'a' goes into index 0, total for 'b' goes into index 1,.... total for 'z' goes into index 25. My hint above should help.

Re: Character Occurence

Do not read the file twice. Doing IO operations are expensive. Read a line once. You can then iterate over it once and do all the necessary checks. Or you can iterate over it as many times as you like (if it is unchanged). Doing multiple loops over the String is better than multiple loops over the file.