Java Reference
In-Depth Information
Note that the method copyText does not perform the letter count, but we still pass
the array letterCount to it. We do this because this method calls the method
chCount , which needs the array letterCount to update the appropriate letter
count. Therefore, we must pass the array letterCount to the copyText method
so that it can pass the array to the method chCount .
static int copyText(FileReader infile, PrintWriter outfile,
int next, int [] letterC) throws IOException
{
while (next != ( int )'\n')
{
outfile.print(( char )(next));
chCount(( char )(next), letterC);
next = infile.read();
}
outfile.println();
return next;
}
chCount This method increments the letter count. To increment the appropriate letter count,
the method must know what the letter is. Therefore, the chCount method has two
parameters: a char variable and the array to update the letter count. In pseudocode,
this method is:
a. Convert the letter to uppercase.
b. Find the index of the array corresponding to this letter.
c. If the index is valid, increment the appropriate count. At this
step, we must ensure that the character is a letter. We are only
counting letters, so other characters—such as commas, hyphens,
and periods—are ignored.
Following this algorithm, the definition of the method is:
static void chCount( char ch, int [] letterC)
{
int index;
ch = Character.toUpperCase(ch);
//Step a
index = ( int ) ch - 65;
//Step b
if (index >= 0 && index < 26)
//Step c
letterC[index]++;
}
 
Search WWH ::




Custom Search