You are on page 1of 1

Jack and Jill have a problem.

They are writing a software program for playing


Scrabble and they need a dictionary to look up words. The most natural way to do
this is to read in a file of words when the program first starts up. Depending on
the size of the dictionary, this file could become quite lengthy, perhaps 50,000
words. Jack's job is to gather the words into a file named words.txt, with one word
per line. He starts out with just seven words, planning to add the other 49,993
words tomorrow or the next day (Example 1-1).

Example 1-1. words.txt File

hill
fetch
pail
water
up
down
crown

Meanwhile, Jill starts coding the Java dictionary object. She uses a very
straightforward approach. When the Dictionary0 object is created, it will read in
the words from the file words.txt and store them in the Vector called "words." A
Vector is a variable-length list of items (Example 1-2).

Example 1-2: Dictionary0.java

import java.io.*;
import java.util.*;

class Dictionary0 {
Vector words = new Vector();

Dictionary0() {
loadWords();
}

void loadWords() {
try {
BufferedReader f = new BufferedReader(

You might also like