10 date 2008.04.11.17.53.47; author rmh3093; state Exp;
15 date 2008.03.31.03.59.36; author rmh3093; state Exp;
34 * $Id: WordSearch.java,v 1.1 2008/03/31 03:59:36 rmh3093 Exp rmh3093 $
37 * $Log: WordSearch.java,v $
38 * Revision 1.1 2008/03/31 03:59:36 rmh3093
45 import java.io.FileNotFoundException;
46 import java.io.IOException;
47 import java.util.Scanner;
50 * The driver program for the word search program. The program expects to find
51 * one command line argument which is used to find the files that contain the
52 * puzzle and the word list. The program will look for a file that has the
53 * suffix .pz for the puzzle, and for a file with the suffix .wl that contains
54 * the word list. A puzzle object is then created using the puzzle file.
55 * After printing out the puzzle and the word list, the program loops through
56 * each word in the word list. If the word is contained in the puzzle, the word
57 * and the location of the word in the puzzle are printed out.
62 public class WordSearch {
64 static Puzzle thePuzzle;
65 static String[] theWords;
67 private static void printWords() {
68 int wordCount = theWords.length;
69 for (int i=0; i<wordCount; i++) {
70 System.out.println(theWords[i]);
74 private static void findWords() {
75 int wordCount = theWords.length;
77 for (int i=0; i<wordCount; i++) {
78 location = thePuzzle.find(theWords[i]);
79 if (location != null ) {
80 System.out.println(theWords[i] + " " + location.toString());
85 private static void getWordList(String wordsFile) throws FileNotFoundException,
86 IOException, PuzzleFileFormatException {
88 // Throw FileNotFoundException if words file does not exist
89 File wl = new File(wordsFile);
90 //System.out.println(wl.exists());
92 throw new PuzzleFileFormatException("WordSearch: cannot open " + wordsFile);
96 Scanner input = new Scanner(wl);
98 int wordCount = Integer.parseInt(input.nextLine());
99 if ((wordCount==0) && input.hasNext()) {
101 throw new PuzzleFileFormatException("WordSearch: invalid word list file");
103 theWords = new String[wordCount];
104 for (int i=0; i<wordCount; i++) {
105 theWords[i] = input.nextLine().toUpperCase();
108 } catch (NumberFormatException e) {
110 throw new PuzzleFileFormatException("WordSearch: invalid word list file");
112 } catch (FileNotFoundException e) {
113 throw new PuzzleFileFormatException("WordSearch: cannot open " + wordsFile);
121 public static void main(String[] args) {
123 if (args.length!=1) {
124 System.err.println("Usage: java WordSearch filename");
129 thePuzzle = new Puzzle(args[0]+".pz");
130 getWordList(args[0]+".wl");
131 } catch (FileNotFoundException e1) {
132 System.out.println(e1);
133 } catch (PuzzleFileFormatException e1) {
134 System.out.println(e1);
135 } catch (IOException e1) {
136 System.out.println(e1);
140 System.out.println();
142 System.out.println();
165 theWords[i] = input.nextLine();