1 public class CountLettersInArray {
2
3 public static void main(String[] args) {
4
5 char[] chars = createArray();
6
7
8 System.out.println("The lowercase letters are:");
9 displayArray(chars);
10
11
12 int[] counts = countLetters(chars);
13
14
15 System.out.println();
16 System.out.println("The occurrences of each letter are:");
17 displayCounts(counts);
18 }
19
20
21 public static char[] createArray() {
22
23 char[] chars = new char[100];
24
25
26
27 for (int i = 0; i < chars.length; i++)
28 chars[i] = RandomCharacter.getRandomLowerCaseLetter();
29
30
31 return chars;
32 }
33
34
35 public static void displayArray(char[] chars) {
36
37 for (int i = 0; i < chars.length; i++) {
38 if ((i + 1) % 20 == 0)
39 System.out.println(chars[i]);
40 else
41 System.out.print(chars[i] + " ");
42 }
43 }
44
45
46 public static int[] countLetters(char[] chars) {
47
48 int[] counts = new int[26];
49
50
51 for (int i = 0; i < chars.length; i++)
52 counts[chars[i] - 'a']++;
53
54 return counts;
55 }
56
57
58 public static void displayCounts(int[] counts) {
59 for (int i = 0; i < counts.length; i++) {
60 if ((i + 1) % 10 == 0)
61 System.out.println(counts[i] + " " + (char)(i + 'a'));
62 else
63 System.out.print(counts[i] + " " + (char)(i + 'a') + " ");
64 }
65 }
66 }