title: How to find all consecutive letters with count from string provided by user?
body: <p>I am trying to write a code in Java which will find all the consecutive letters in string provided by user and also provide its count.</p>
  
  <p>For example:
  User has provided string: "aaastt rr".</p>
  
  <p>I am expecting the result as below:</p>
  
  <p>a - 3</p>
  
  <p>t - 2</p>
  
  <p>r - 2</p>
  
  <p>I have written below code as per my understanding but not getting the result as expected.</p>
  
  <pre><code>import java.util.Scanner;
  public class ConsecutiveCharacters {
  public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter string: ");
      char s[] = sc.nextLine().toCharArray();
      int count = 1;
      for(int i =0;i&lt;s.length-1;i++){
          if(s[i]==s[i+1]){
              count++;
              System.out.println(s[i] + "-" + count);
  
          }
      }
  
  }
  }
  </code></pre>
  
  <p>I am getting result:</p>
  
  <p>a-2</p>
  
  <p>a-3</p>
  
  <p>t-4</p>
  
  <p>r-5</p>
  
  <p>which is not I am expecting.</p>
  
  <p>Please have a look, and let me know where I am missing.</p>
  
  <p>Many Thanks in advance.</p>