public class WordSearch3 { public static void main (String[] argv) { char[][] puzzle = { {'n', 'o', 'h', 't', 'y', 'p', 's'}, {'m', 'i', 'a', 'r', 'y', 'c', 'c'}, {'l', 'l', 'e', 'k', 's', 'a', 'h'}, {'r', 'u', 'b', 'y', 'v', 'm', 'e'}, {'e', 'h', 'h', 'a', 'l', 'l', 'm'}, {'p', 'c', 'j', 'n', 'i', 'c', 'e'}, {'r', 'e', 'e', 'k', 'b', 'i', 'p'} }; findWord (puzzle, "ruby"); } static void findWord (char[][] puzzle, String word) { char[] letters = word.toCharArray (); // Use a single flag variable. boolean found = false; for (int i=0; (i= puzzle[i].length) || (letters[k] != puzzle[i][j+k]) ) { // Not a match. found = false; } } // If we went the whole length of the word, we found it. if (found) { System.out.println ("String " + word + " found in row=" + i + " col=" +j); } } } if (! found) { System.out.println ("String " + word + " not found"); } } }