title: Array Index Out Of Bounds - Java Recursive Method
body: <p>I am writing a program with 3 different methods to practice recursion. I have successfully implemented one of three, but I am a little stuck on the second right now. This method attempts to count the number of "smiles" (:)) in a character array. I thought I had it written correctly but when I test it out I keep getting an ArrayIndexOutOfBoundsException, but I'm not sure why. My method is as follows:</p>
  
  <pre><code>public static int countSmiles(char[] letters, int index) {
  
      int smileCounter = 0;
  
      //If the array is less than 2 in length it cannot include ":)".
      if(letters.length &lt; 2) {
          return 0;
      }
  
      //If there is a smile increment the counter.
      else if(letters[index] == ':') {
          if(letters[index+1] == ')') {
              smileCounter++;
          }
      }
  
      //Increment the index
      index++;
  
      //Recursive assignment
      smileCounter = countSmiles(letters, index);
  
      //Return number of smiles
      return smileCounter;
  
  }
  </code></pre>
  
  <p>And the method I am testing with is as follows:</p>
  
  <pre><code>public static void main(String[] args) {
  
      char[] JavaCharArray = {'r', 's', 't', 'u', ':', ')', 'v'};
      System.out.println(countSmiles(JavaCharArray, 0));
  }
  </code></pre>
  
  <p>From my code it doesn't seem that the index I am trying to access (0) is negative nor greater than the provided array. I really just don't understand.</p>