...
[rmh3093.git] / project1 / WordSearch.java
blob885b2c4a27aeba46cfc01ae8e745c3dc5faba70c
1 /*
2 * WordSearch.java
4 * Version:
5 * $Id: WordSearch.java,v 1.2 2008/04/11 17:53:47 rmh3093 Exp rmh3093 $
7 * Revisions:
8 * $Log: WordSearch.java,v $
9 * Revision 1.2 2008/04/11 17:53:47 rmh3093
10 * finding words works
12 * Revision 1.1 2008/03/31 03:59:36 rmh3093
13 * Initial revision
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.util.Scanner;
23 /**
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.
33 * @author rmh3093
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;
50 Location location;
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());
65 if (!wl.exists()) {
66 throw new PuzzleFileFormatException("WordSearch: cannot open " +
67 wordsFile);
70 try {
71 Scanner input = new Scanner(wl);
72 try {
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");
79 } else {
80 theWords[i] = input.nextLine();
83 if (input.hasNext()) {
84 throw new PuzzleFileFormatException("WordSearch: " +
85 "invalid word list file");
87 input.close();
88 } catch (NumberFormatException e) {
89 input.close();
90 throw new PuzzleFileFormatException("WordSearch: " +
91 "invalid word list file");
93 } catch (FileNotFoundException e) {
94 throw new PuzzleFileFormatException("WordSearch: cannot open " +
95 wordsFile);
101 * @param args
103 public static void main(String[] args) {
105 if (args.length!=1) {
106 System.err.println("Usage: java WordSearch filename");
108 } else {
110 try {
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);
121 thePuzzle.print();
122 System.out.println();
123 printWords();
124 System.out.println();
125 findWords();