title: Recursion to find the number of occurrences of a specified character in a string
body: <p>I have seen the solution to this question here, but the persons code is different from mine. I just want to know what I am doing wrong. I am very new to programming and desperate to be good.</p>
  
  <p>Question: (Occurrences of a specified character in a string) Write a recursive method that finds the number of occurrences of a specified character in a string using the following header:</p>
  
  <pre><code>public static int count(String str, char a)
  </code></pre>
  
  <p>For example: <code>count("Welcome", 'e')</code> returns <code>2</code>. Write a test program that prompts the user to enter a string followed by a character and displays the number of occurrences of the character in the string. </p>
  
  <p>Issue with my code:  </p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1</p>
  </blockquote>
  
  <p>My Code:</p>
  
  <pre><code>import java.util.Scanner;
  public class Exercise18_10{
      public static void main(String[] args){
          Scanner input = new Scanner(System.in);
          System.out.println("Please enter a string");   
          String str = input.next();
          System.out.println("Please enter a character");   
          char car = input.next().charAt(0);
          int x = count ("welcome", 'e');
          System.out.println("The number of character " +car + " in string " +str + "     = "+ x);
      }//main
  
      public static int count(String str, char a){
          int ct = 0;    
          if(str.length()&gt;0)
              if(str.charAt(0) == a)
                  ct ++;
          count(str.substring(1),a);
          return ct;
      }
  }
  </code></pre>
  
  <p>Help please, what am I doing wrong? </p>