Here's the way that I see it:
You need an array of 26 integers. Each element of the array will hold the count of a particular character 'a' through 'z'. You could call the array
counters. The counters array is declared and initialized in the LetterInventory class.
counters[0] will hold the count for the character 'a'
counters[1] will hold the count for the character 'b'
.
.
.
counters[25] will hold the count for the character 'z'
Then in the
countOccurrences method of that class:
Make a loop that reads lines from the given Scanner file. Each line is read into a String.
A conventient form of loop can be something like:
while (file.hasNextLine()) {
String line = file.nextLine();
// Do something with the String
}
What is that "something that you do with the String?
Well...
Convert each String read by the Scanner nextLine() method to lower case and then do something like the following
Make a loop that goes through all of the characters in the String.
Inside the loop it goes something like this:
Use the charAt() method to retrieve the current character
Declare an integer variable named x and set x equal to the integer value of the current character minus 'a'.
(That means that 'a' becomes 0, 'b' becomes 1, ... , 'z' becomes 25)
If x is greater than or equal to zero and x is less than the number of elements in the counters array, then
Increment counters[x]
End if
(That means that spaces and punctuation and everything other than a alphabetic characters will be ignored.)
Finally...
The displayTable method would print out the value of each of the counters in some kind of loop like
for (int i = 0; i < counters.length; i++) {
System.out.printf("%c: %5d\n", (char)(i + 'a'), counters[i]);
}
So a file containing the single line "The quick brown fox jumps over the lazy dog." would have the following output:
a: 1
b: 1
c: 1
d: 1
e: 3
f: 1
g: 1
h: 2
i: 1
j: 1
k: 1
l: 1
m: 1
n: 1
o: 4
p: 1
q: 1
r: 2
s: 1
t: 2
u: 2
v: 1
w: 1
x: 1
y: 1
z: 1
Cheers!
Z