title: word search in java 2d array
body: <p>I am trying to create a simple word search for a class assignment, and I have managed to figure out how to search east (from left to right) and west(right to left). But I am having trouble trying to figure out how to search south (top to bottom).</p>
  
  <p>The code that I have works for one file that I read in but the second file returns an <code>ArrayIndexOutOfBoundsException</code>. Is there anything that is specific in my code that would make it un-scalable?</p>
  
  <p>My <strong>corrected</strong> code looks like this:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.ArrayList;
  import java.util.Scanner;
  
  import wordSeek.GameBoard;
  
  public class WordGame {
  
      private char[][] letters;
      GameBoard gb;
  
      public static void main(String[] args) {
          WordGame wg = new WordGame();
          wg.play();
  
      }
  
      public WordGame() {
          letters = readLettersFromFile();
          gb = new GameBoard(letters);
  
      }
  
      private void play() {
          Scanner s = new Scanner(System.in);
          String word;
  
          do {
              System.out.println("Enter word to find: ");
              word = s.next();
  
              // reset all highlighted tiles
              gb.reset();
  
              search(word);
  
          } while (!word.equals("QUIT"));
  
          gb.dispose();
      }
  
      // Nothing to be done above
      // Complete all the methods below
  
      private char[][] readLettersFromFile() {
          // From the data in the file Letters.txt determine the size (number of
          // rows and number of columns) for the letters array
  
          int rowCount = 0;
          int colCount = 0;
          char c;
  
          File file = new File("resources/Places.txt");
  
          Scanner fileScanner = null;
          try {
              fileScanner = new Scanner(file);
              } 
          catch (FileNotFoundException e)
              {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          ArrayList&lt;String&gt; data = new ArrayList&lt;String&gt;();
  
          while(fileScanner.hasNextLine())
          {
              String line = fileScanner.nextLine();
              data.add(line);
          }
  
          fileScanner.close();
          rowCount = data.size();
          colCount = data.get(0).trim().length()/2+1;
  
          // Instantiate a two dimensional array of characters of the appropriate
          // size
  
          letters = new char [rowCount][colCount];
  
          // Populate the array with the letters in Letters.txt
          for (int i = 0; i &lt; rowCount; i++) {
              String line = data.get(i);
              int pos = 0;
              for (int j = 0; j &lt; colCount; j++) {
                  letters[i][j] = line.charAt(pos);
                  pos += 2;
              }
  
          }
  
          // return the array
  
          return letters;
  
      }
  
      private void search(String word) {
          System.out.println("Searching for " + word);
  
          //Call the other search methods below as needed
          searchIterativeEast(word);
          searchIterativeWest(word);
          searchIterativeSouth(word);
          searchIterativeNorth(word);
      }
  
      //The following four methods must employ ITERATION to search the game board
      private boolean searchIterativeEast(String word) {
          int k = 0;
  
          for (int i = 0; i &lt; letters.length; i++) 
          {
              for (int j = 0; j &lt; letters[i].length; j++) {
                  if (word.charAt(k) == letters[i][j]) {
                      k++;
                  } 
                  else {
                      k = 0;
                  }
                  if (k == word.length()) {
                      for (int col = j - k + 1; col &lt;= j; col++) {
                          gb.highlight(i, col);
                      }
  
                      return true;
                  }
              }
          }
  
          return false;
      }
  
      private boolean searchIterativeWest(String word) {
  
              String reversedWord = "";
              for (int i = word.length() - 1; i != -1; i--)
              {
                  reversedWord += word.charAt(i);
              }
  
              int k = 0;
  
              for (int i = 0; i &lt; letters.length; i++) 
              {
                  for (int j = 0; j &lt; letters[i].length; j++) {
                      if (reversedWord.charAt(k) == letters[i][j]) {
                          k++;
                      } 
                      else {
                          k = 0;
                      }
                      if (k == reversedWord.length()) {
                          for (int col = j - k + 1; col &lt;= j; col++) {
                              gb.highlight(i, col);
                          }
  
                          return true;
                      }
                  }
              }
  
              return false;
  
      }
  
      private boolean searchIterativeSouth(String word) {
          int k = 0;
          int store = letters[0].length;
  
          for (int j = 0; j &lt; letters[store].length; j++)
          {
              for (int i = 0; i &lt; letters.length; i++)
              {
                  if (word.charAt(k) == letters[i][j])
                  {
                      k++;
                  }
                  else
                  {
                      k = 0;
                  }
                  if (k == word.length())
                  {
                      for(int row = i-k+1 ; row &lt;= i; row++)
                      {
                          gb.highlight(row, j);
                      }
                      return true;
                  }
              }
          }
  
          return false;
      }
  
      private boolean searchIterativeNorth(String word) {
  
          String reversedWord = "";
          for (int i = word.length() - 1; i != -1; i--)
          {
              reversedWord += word.charAt(i);
          }
  
          int k = 0;
          int store = 0;
  
          for(int i = 0; i &lt; letters.length; i++)
          {
              store = letters[i].length;
          }
  
          for (int j = 0; j &lt; letters[store].length; j++)
          {
              for (int i = 0; i &lt; letters.length; i++)
              {
                  if (reversedWord.charAt(k) == letters[i][j])
                  {
                      k++;
                  }
                  else
                  {
                      k = 0;
                  }
                  if (k == reversedWord.length())
                  {
                      for(int row = i-k+1 ; row &lt;= i; row++)
                      {
                          gb.highlight(row, j);
                      }
                      return true;
                  }
              }
          }
  
          return false;
      }
  </code></pre>
  
  <p>The Gameboard for the first file (Animals.txt) looks like: 5X4 2d Array</p>
  
  <pre><code>X C A T
  P A L Q
  I R B U
  G P X N
  G O D W
  </code></pre>
  
  <p>Output highlights CARP.</p>
  
  <p>The Gameboard for the second file (Places.txt) looks like: 11x15 2d Array</p>
  
  <pre><code>O M J G D A X V C S Q N K I F 
  D A X V T Q O M J H A A H F C 
  A Y W U R P N L F E I T A L Y
  J N H N E T H E R L A N D S F 
  D B I Z X V T O A R Q O A Y K 
  M K I A H F K R N O D B N N I
  N Z Y W P H T V C G C T A A N 
  R A Q O T S N L E K K I C M G 
  I H P U U F D C A Z D O X R D 
  X W O A L E U Z E N E V N E O 
  V S U S J R Q L I Z A R B G M 
  </code></pre>