/** * Word Search * Input is a puzzle in a file and words to find in the puzzle * The puzzle file name can be provided on the command line or * as the first input from the user. * Puzzle file must contain the numberOfRows and numberOfColumns * followed by numberOfRows strings of numberOfColumns characters. * These may be on one line or multiple lines. * For example: * 2 3 * abc * def * Author: Beth Katz and Blaise Liffick * January 2008 */ import java.io.*; import java.util.*; public class WordSearch { public static final char BORDER = '='; static Scanner console = new Scanner(System.in); public static void main(String[] args) { String filename = null; Scanner puzzleFile; try { if (args.length > 0) { // file name is on command line filename = args[0]; } else { // file name is typed by user System.out.println("Enter puzzle file name: "); filename = console.next(); } puzzleFile = new Scanner(new File(filename)); char[][] puzzle = readPuzzle(puzzleFile); puzzleFile.close(); // close when no longer needed System.out.println(puzzle.length + " " + puzzle[0].length); printPuzzle(puzzle); } catch (FileNotFoundException e) { System.out.println("File " + filename + " could not be opened"); } catch (NoSuchElementException e) { System.out.println("No file name obtained from user"); } catch (IllegalStateException e) { // handle many other exceptions here System.out.println(e.getMessage()); } } // ----------------------------------------------------- /** * reads the puzzle from the already opened puzzleFile */ static char[][] readPuzzle(Scanner puzzleFile) { int numRows; int numCols; String exceptionMessage1 = "Puzzle file must contain the numberOfRows and numberOfColumns\n" + "followed by numberOfRows strings of numberOfColumns characters.\n" + "For example: 2 3 abc def"; String exceptionMessage2 = "Rows must be between 1 and 20 (inclusive) but is "; String exceptionMessage3 = "Columns must be between 1 and 20 (inclusive) but is "; try { numRows = puzzleFile.nextInt(); // System.out.println(numRows + " rows in the puzzle"); numCols = puzzleFile.nextInt(); // System.out.println(numCols + " columns in the puzzle"); if (numRows < 1 || numRows > 20) throw new IllegalStateException(exceptionMessage2 + numRows); if (numCols < 1 || numCols > 20) throw new IllegalStateException(exceptionMessage3 + numCols); char[][] puzzle = new char[numRows + 2][numCols + 2]; initPuzzle(puzzle); for (int row = 1; row < puzzle.length - 1; row++) { String s = puzzleFile.next(); for (int col = 1; col < puzzle[row].length - 1; col++) { puzzle[row][col] = s.charAt(col - 1); } } return puzzle; } catch (NoSuchElementException e) { throw new IllegalStateException(exceptionMessage1); } catch (StringIndexOutOfBoundsException e) { throw new IllegalStateException(exceptionMessage1); } } //---------------------------------------- // This method prints out a puzzle, including // the border. static void printPuzzle (char[][] p) { for (int r = 0; r < p.length; r++) { for (int c = 0; c < p[r].length; c++) { System.out.print(p[r][c] + " "); } System.out.println(); } System.out.println(); } //---------------------------------------- // This method initializes a puzzle by setting // the array's entire contents to "=", which // acts as a border for the puzzle processing. static void initPuzzle (char[][] p) { for (int r = 0; r < p.length; r++) { for (int c = 0; c < p[r].length; c++) { p[r][c] = BORDER; } } } }