title: String Index Out of Bounds Error
body: <p>The following code is attempting to Given a string, compute recursively (no loops) the number of lowercase 'x' chars in the string.</p>
  
  <p>The code is having this error: 
  Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 </p>
  
  <p>The main method for this code is:</p>
  
  <pre><code>public static void main(String [] args)
  {
    System.out.println(countX("hx1x"));
  }
  </code></pre>
  
  <p>The actual code is:</p>
  
  <pre><code>public static int countX(String str)
  { 
      if(str.charAt(0) != 'x')
      {
          if(str.indexOf('x') &gt;= 1)
          {
              return countX(str.substring(1, str.length()));
          }
          else
          {
              return 0;
          }
      }
      else
      {
          return 1 + countX(str.substring(1, str.length()));
      }
  }
  </code></pre>