title: Counting the number of occurences of characters in a string
body: <p>I am trying to write a Java program which takes in as input a string and counts the number of occurrences of characters in a string and then prints a new string having the character followed by the no of occurrences.</p>
  <h2>E.G.</h2>
  <p>Input String:</p>
  <pre><code>aaaabb
  </code></pre>
  <p>Output String:</p>
  <pre><code>a4b2
  </code></pre>
  <p>Input String:</p>
  <pre><code>aaaaabbbc
  </code></pre>
  <p>Output String:</p>
  <pre><code>a5b3c1
  </code></pre>
  <p>I am posting my java code.<br />
  It is throwing <code>StringOutOfBoundException</code></p>
  <pre><code>/*Write a routine that takes as input a string such as &quot;aabbccdef&quot; and o/p &quot;a2b2c2def&quot; or &quot;a4bd2g4&quot; for &quot;aaaabddgggg&quot;.*/
  
  import java.util.Scanner;
  
  public class CountingOccurences {
  
  public static void main(String[] args) {
  
      Scanner inp= new Scanner(System.in);
      String str;
      char ch;
      int count=0;
      
      System.out.println(&quot;Enter the string:&quot;);
      str=inp.nextLine();
      
      while(str.length()&gt;0)
      {
          ch=str.charAt(0);
          int i=0;
          
          while(str.charAt(i)==ch)
          {
                  count =count+i;
                  i++;
          }
          
          str.substring(count);
          System.out.println(ch);
          System.out.println(count);
      }
  
  }
  
  }
  </code></pre>