title: java.lang.ArrayIndexOutOfBoundsException: 18
body: <p>I need to solve a crossword puzzle and one method is to put a word in the puzzle.
  But when I run it, I get the following error:</p>
  
  <pre><code> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18
  at CrosswordPuzzleSolver.setWord(CrosswordPuzzleSolver.java:224)
  </code></pre>
  
  <p>This is my code:</p>
  
  <pre><code>  public static char[][] setWord(int[] gridSpecEntry, String wordsSpecEntry,
          char[][] grid) {
          if (wordsSpecEntry.length()&gt;0&amp;&amp;wordsSpecEntry.length()==gridSpecEntry[2])
          {
          int y = gridSpecEntry[0]; //y-coordinate
          int x = gridSpecEntry[1]; //x-coord
          int w = gridSpecEntry[2]; //length of word
          int r = gridSpecEntry[3]; //horizontal or vertical
          if (r == 0) { //if is 0, you need to put in the word horizontal
              for (int b = 0; b &lt; wordsSpecEntry.length(); b++) {
                  grid[y][x + b] = wordsSpecEntry.charAt(b); //line 224
              }
          } else {
              for (int a = 0; a &lt; wordsSpecEntry.length(); a++) {
                  grid[y + a][x] = wordsSpecEntry.charAt(a);
              }
          }
      }
      return grid;
  }
  </code></pre>
  
  <p>Can someone help me, please?</p>