title: Find the longest sequence of same characters in a string
body: <p>This code supposed to output the longest run on which a character in a string has a consecutive runs of itself. Though the problem is that it outputs: <em>8</em> (which should be <em>5</em> instead). I just would like to ask what seems to be the problem regarding this code.</p>
  
  <pre><code>public class Sample {
      public static void main(String[] args) {
          String setofletters = "aaakkcccccczz"; /* 15 */
          int output = runLongestIndex(setofletters);
          System.out.println("Longest run that first appeared in index:" + output);
      }
  
      public static int runLongestIndex(String setofletters) {
          int ctr = 0;
          int ctrstor = 0;
          int ii = 0;
          int output = 0;
  
          // loops until the last character in the string
          for (int i = 0; i &lt; setofletters.length() - 1; i++) {
              // checks if the letter is the same to the next
              if (setofletters.charAt(i) == setofletters.charAt(i++)) {
                  ctr++;
                  ii = i++;
                  // loops until the letter in the index is no longer equal
                  while (setofletters.charAt(i) == setofletters.charAt(ii)) {
                      ii++;
                      ctr++;
                  }
  
                  if (ctr &gt; ctrstor) {
                      output = i;
                  }
                  // storing purposes
                  ctrstor = ctr;
              }
              // resets the counter
              ctr = 0;
          }
          return output;
      }
  }
  </code></pre>