jhdsf
[rmh3093.git] / project1 / RCS / WordSearch.java,v
blob42ab3cfeb1cd23e32d6f7006eb69e93aaecc9d31
1 head    1.2;
2 access;
3 symbols;
4 locks
5         rmh3093:1.2; strict;
6 comment @# @;
9 1.2
10 date    2008.04.11.17.53.47;    author rmh3093; state Exp;
11 branches;
12 next    1.1;
14 1.1
15 date    2008.03.31.03.59.36;    author rmh3093; state Exp;
16 branches;
17 next    ;
20 desc
21 @initial commit.
25 1.2
26 log
27 @finding words works
29 text
30 @/*
31  * WordSearch.java
32  *
33  * Version:
34  *     $Id: WordSearch.java,v 1.1 2008/03/31 03:59:36 rmh3093 Exp rmh3093 $
35  *
36  * Revisions:
37  *     $Log: WordSearch.java,v $
38  *     Revision 1.1  2008/03/31 03:59:36  rmh3093
39  *     Initial revision
40  *
41  */
44 import java.io.File;
45 import java.io.FileNotFoundException;
46 import java.io.IOException;
47 import java.util.Scanner;
49 /**
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.
58  *  
59  * @@author rmh3093
60  *
61  */
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]);
71                 }
72         }
73         
74         private static void findWords() {
75                 int wordCount = theWords.length;
76                 Location location;
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());
81                         }
82                 }
83         }
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());
91                 if (!wl.exists()) {
92                         throw new PuzzleFileFormatException("WordSearch:  cannot open " + wordsFile);
93                 }
95                 try {
96                         Scanner input = new Scanner(wl);
97                         try {
98                                 int wordCount = Integer.parseInt(input.nextLine());
99                                 if ((wordCount==0) && input.hasNext()) {
100                                         input.close();
101                                         throw new PuzzleFileFormatException("WordSearch:  invalid word list file");
102                                 }
103                                 theWords = new String[wordCount];
104                                 for (int i=0; i<wordCount; i++) {
105                                         theWords[i] = input.nextLine().toUpperCase();
106                                 }
107                                 input.close();
108                         } catch (NumberFormatException e) {
109                                 input.close();
110                                 throw new PuzzleFileFormatException("WordSearch:  invalid word list file");
111                         }
112                 } catch (FileNotFoundException e) {
113                         throw new PuzzleFileFormatException("WordSearch:  cannot open " + wordsFile);
114                 }
116         }
118         /**
119          * @@param args
120          */
121         public static void main(String[] args) {
123                 if (args.length!=1) {
124                         System.err.println("Usage:  java WordSearch filename");
125                         
126                 } else {
128                         try {
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);
137                         }
139                         thePuzzle.print();
140                         System.out.println();
141                         printWords();
142                         System.out.println();
143                         findWords();
144                 }
145         }
153 @Initial revision
155 text
156 @d5 1
157 a5 1
158  *     $Id$
159 d8 4
160 a11 1
161  *     $Log$
162 d44 11
163 d76 1
164 a76 1
165                                         theWords[i] = input.nextLine();
166 d114 1
167 a114 1
168