5 * $Id: WordSearch.java,v 1.2 2008/04/11 17:53:47 rmh3093 Exp rmh3093 $
8 * $Log: WordSearch.java,v $
9 * Revision 1.2 2008/04/11 17:53:47 rmh3093
12 * Revision 1.1 2008/03/31 03:59:36 rmh3093
19 import java
.io
.FileNotFoundException
;
20 import java
.io
.IOException
;
21 import java
.util
.Scanner
;
24 * The driver program for the word search program. The program expects to find
25 * one command line argument which is used to find the files that contain the
26 * puzzle and the word list. The program will look for a file that has the
27 * suffix .pz for the puzzle, and for a file with the suffix .wl that contains
28 * the word list. A puzzle object is then created using the puzzle file.
29 * After printing out the puzzle and the word list, the program loops through
30 * each word in the word list. If the word is contained in the puzzle, the word
31 * and the location of the word in the puzzle are printed out.
36 public class WordSearch
{
38 static Puzzle thePuzzle
;
39 static String
[] theWords
;
41 private static void printWords() {
42 int wordCount
= theWords
.length
;
43 for (int i
=0; i
<wordCount
; i
++) {
44 System
.out
.println(theWords
[i
]);
48 private static void findWords() {
49 int wordCount
= theWords
.length
;
51 for (int i
=0; i
<wordCount
; i
++) {
52 location
= thePuzzle
.find(theWords
[i
]);
53 if (location
!= null ) {
54 System
.out
.println(theWords
[i
] + " " + location
.toString());
59 private static void getWordList(String wordsFile
) throws
60 FileNotFoundException
, IOException
, PuzzleFileFormatException
{
62 // Throw FileNotFoundException if words file does not exist
63 File wl
= new File(wordsFile
);
64 //System.out.println(wl.exists());
66 throw new PuzzleFileFormatException("WordSearch: cannot open " +
71 Scanner input
= new Scanner(wl
);
73 int wordCount
= Integer
.parseInt(input
.nextLine());
74 theWords
= new String
[wordCount
];
75 for (int i
=0; i
<wordCount
; i
++) {
76 if (!input
.hasNext()) {
77 throw new PuzzleFileFormatException("WordSearch: " +
78 "invalid word list file");
80 theWords
[i
] = input
.nextLine();
83 if (input
.hasNext()) {
84 throw new PuzzleFileFormatException("WordSearch: " +
85 "invalid word list file");
88 } catch (NumberFormatException e
) {
90 throw new PuzzleFileFormatException("WordSearch: " +
91 "invalid word list file");
93 } catch (FileNotFoundException e
) {
94 throw new PuzzleFileFormatException("WordSearch: cannot open " +
103 public static void main(String
[] args
) {
105 if (args
.length
!=1) {
106 System
.err
.println("Usage: java WordSearch filename");
111 thePuzzle
= new Puzzle(args
[0]+".pz");
112 getWordList(args
[0]+".wl");
113 } catch (FileNotFoundException e1
) {
114 System
.out
.println(e1
);
115 } catch (PuzzleFileFormatException e1
) {
116 System
.out
.println(e1
);
117 } catch (IOException e1
) {
118 System
.out
.println(e1
);
122 System
.out
.println();
124 System
.out
.println();