Java Reference
In-Depth Information
import java.io.IOException;
public class Driver
{
public static void main(String[] args)
{
FrequencyCounter wordCounter = new FrequencyCounter();
String fileName = "Data.txt"; // or file name could be read
try
{
Scanner data = new Scanner( new File(fileName));
wordCounter.readFile(data);
}
catch (FileNotFoundException e)
{
System.out.println("File not found: " + e.getMessage());
}
catch (IOException e)
{
System.out.println("I/O error " + e.getMessage());
}
wordCounter.display();
System.out.println("Bye!");
} // end main
} // end Driver
Output
boat 1
row 3
your 1
19.13
Is the ADT dictionary the right one to use for this problem? A word and its frequency of occurrence
in the document form a pair that is suitable as an entry in a dictionary. If we want to know a given
word's frequency, the word should be the search key. Also, the words in the dictionary must be dis-
tinct, and if they are sorted, we can display them in alphabetical order. Thus, a sorted dictionary
with distinct search keys is an appropriate choice for this problem. As in the previous example, we
assume SortedDictionary is such an implementation.
The dictionary will be a data field of a new class FrequencyCounter , which will begin much
like the class TelephoneDirectory in the previous example. Let's call the dictionary for this exam-
ple wordTable . Since the value portion of any dictionary entry is an object, we use the wrapper
class Integer to represent each frequency. Thus, our class can begin as shown in Listing 19-5.
LISTING 19-5
An outline of the class FrequencyCounter
import java.util.Iterator;
import java.util.Scanner;
Search WWH ::




Custom Search