title: Count instances of 'a' using recursion
body: <p>I have a homework assignment for beginning Java to count the instances of 'a' in a word or phrase.  I am receiving the following errors.</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of >range: 4
      at java.lang.String.charAt(String.java:658)
      at aHomework.aHomework.countAInWord(aHomework.java:26)
      at aHomework.aHomework.main(aHomework.java:13)
    Java Result: 1</p>
  </blockquote>
  
  <p>Here is the code:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class aHomework 
  {
  public static void main( String [] args )
  {
      Scanner inputW = new Scanner(System.in);
  
      String input = inputW.nextLine();
      countAInWord(input, input.length());    
  }
  
  public static void countAInWord( String w , int n)
     {
  
   int aCount = 0;
   if ( n &gt; 0 )
   {
       if (w.charAt(n) == 'a')
       {
           aCount++;
       }
       else
       {
           n = n-1;
           countAInWord(w, n);
       }
   }
   else
       System.out.println("The number of A's is " + aCount);
     }
  }
  </code></pre>
  
  <p>I have been searching for recursion problems and have learned a lot.  </p>
  
  <p>However I would prefer to fix my code instead of just coping someone elses.</p>
  
  <p>So if someone would be so kind as to tell me why I am receiving the above errors I would greatly appreciate it.</p>