//**************************************************************************** // Dictionary.java file for Spring 2006, CS1501 Project 1: Anagrams // This class has a method that reads a list of words in from a dictionary file // into an array, and a method that prints the contents of the array out to // standard output. // (This code was adapted from a Spring 2006 student project submission.) //**************************************************************************** import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public class Dictionary { private String[] wordlist; private int numwords; private String filename; //File must have each word on a new line and end with a blank line //******************************************************************** // Name: Dictionary (constructor) // Description: inputs dictionary's filename and initializes fields // (File must have each word on a new line and end with a blank line.) //******************************************************************** public Dictionary(String fname) { filename = fname; //dictionary filename //count words in the file numwords = 0; try { File infile = new File(filename); BufferedReader fReader; //get ready to read from the file fReader = new BufferedReader(new FileReader(infile)); // string to hold word extracted from file String nextword = ""; // read in first word from file nextword = fReader.readLine(); // Do this until you reach an empty line (which is the eof sentinel). while(nextword != null) { numwords++; nextword = fReader.readLine(); } } catch(Exception e) { System.out.println(e); } //initialize array to size that will hold the words wordlist = new String[numwords]; }//end constructor //******************************************************************** // Name: buildFromFile // Description: Adds a list of words from the file to the wordlist array // (File must have each word on a new line and end with a blank line.) //******************************************************************** public void buildFromFile() { try { File infile = new File(filename); BufferedReader fReader; //get ready to read from the file fReader = new BufferedReader(new FileReader(infile)); // string to hold word extracted from file String nextword = ""; int wordcount = 0; // read in first word from file nextword = fReader.readLine(); // Do this until you reach an empty line (which is the file sentinel). while(nextword != null) { // Insert the new_word into the wordlist array. wordlist[wordcount] = nextword; // Then read in the next word from the file. nextword = fReader.readLine(); wordcount++; } } catch(Exception e) { System.out.println(e); } }//end method buildFromFile //******************************************************************** // Name: printWordList // Outputs the words from the array wordlist to standard output. //******************************************************************** public void printWordList() { for (int w = 0; w < numwords; w++){ System.out.println(wordlist[w]); } } //Here is just some demo code. This method should be modified or omitted. public static void main (String[] args) { Dictionary d = new Dictionary("5desk.txt"); d.buildFromFile(); d.printWordList(); } }