// Description: 1) Input the characters(bytes) from a file containing
// normal (ASCII) text, with both upper and lower case.
// 2) For each byte, determine if it's a letter. Convert
// it to UPPER CASE. Then "count" it. That is, count
// how many how many 'A's, 'B's, 'C's, etc. All upper
// case, makes the counting easier. Count the letters in
// the entire file.
// 3) Output the results of the counting as a table which
// looks like:
// Letter Frequency
// A 23
// B 14
// etc...
// 4) Design your program so the file name is input through
// the command line in MSDos.
import java.io.* ;
public class CountReader
{
Reader in = null;
int rread = 0, charCount=0;
boolean whiteSpace = false;
public CountReader (Reader r)
{
in = r;
}
/**
* Implementation for parent's read method. Counts chars.
*/
public int read(char[] array, int[] cnt) throws IOException
{
if (array == null)
throw new IOException("Null array");
// Do actual read
rread = in.read(array);
// Now count
charCount += rread; // Increment character count
char c;
for (int i = 0; i < rread; i++)
{
c = Character.toUpperCase(array[i]);
if( Character.isLetter(c) )
cnt[(int)c-65]++;
}
return rread;
}
public void close() throws IOException
{
in.close();
}
public int getCharCount()
{
return charCount;
}
/** Test driver */
public static void main(String args[]) throws Exception
{
CountReader r = new CountReader(new FileReader("sample1.txt"));
char c[] = new char[4096];
int re = 0;
int[] lcount = new int[26];
for(int i=0; i < 26; i++)
lcount[i] = 0;
while ((re = r.read(c, lcount)) != -1)
System.out.print(new String(c, 0, re));
r.close();
System.out.println("\n\nRead chars: " + r.getCharCount());
for(int i=0; i < 26; i++)
System.out.println( " " + (char)(i+65) + '\t' + lcount[i] );
}
}
/* Output
C:\>a:
A:\>java CountReader sample.txt
If we looked only by
what we know,
we could not turn our
heads:
If we were at the
mercy of what
we understand,
our eyes could not see:
discovery is
praise &
understandig is
celebration:
Read chars: 195
A 8
B 2
C 5
D 9
E 21
F 3
G 1
H 4
I 8
J 0
K 2
L 5
M 1
N 10
O 13
P 1
Q 0
R 10
S 9
T 10
U 7
V 1
W 9
X 0
Y 5
Z 0
A:\lab3>java CountReader sample1.txt
In a classic story, the famous mathematician Archimedes was asked
to determined if a golden crown commissioned by the king was indeed
pure gold, and not part silver, as an informant had claimed.
Archimedes discovered a way to determine this while stepping into a
(GreeK) bath. He noted that water spilled out of the bath in
proportion to the amount of him that went in. Realizing the
implications of this fact, he immediately got out of the bath and ran
naked through the city shouting, "Eureka, eureka!," for he had
discovered an analysis tool. (displacement), which combined with
a simple scale, could determine if the king's new crown was good
or not. This discovery was unfortunate for the goldsmith, however,
for when Archimedes did his analysis, the crown displaced more
water than an equal-weight lump of pure gold, indicating that the
crown was not, in fact, pure gold.
Read chars: 898
A 60
B 5
C 26
D 39
E 76
F 15
G 16
H 43
I 59
J 0
K 7
L 24
M 24
N 49
O 53
P 14
Q 1
R 37
S 38
T 64
U 16
V 5
W 20
X 0
Y 8
Z 1
A:\lab3>
*/