title: Count lexemes - string out of bounds exception
body: <p>I have a small program that is suppose to count lexemes. What I get is an erorr. I am fairly new to Java. Here is the error:</p>
  
  <pre><code>c:\programming&gt;java LexemesTokenizer
  input string: a = ++b; c = d - /* -e */ --f;
  Input string= a
  Delimeter=  .
  Total number of delimeters= 2 Exception in thread "main" java.lang.StringIndexOu
  tOfBoundsException: String index out of range: 2
      at java.lang.String.charAt(Unknown Source)
      at LexemesTokenizer.main(LexemesTokenizer.java:41)
  </code></pre>
  
  <p>Here is the code</p>
  
  <pre><code>import java.util.Scanner;
  
  public class LexemesTokenizer {
  
      public static void main(String[] args) {
  
          Scanner scanner = new Scanner(System.in);
  
          System.out.print("input string: ");
          String s = scanner.next();
  
          char delimiter = ' ';
  
          // calculate number of delimiter characters
          int cntDelimeters = 0;
          for (int i = 0; i &lt; s.length(); i++) {
              if (s.charAt(i) != delimiter) {
                  cntDelimeters++;
              }
          }
  
          System.out.println("Input string= " + s);
          System.out.printf("Delimeter= %c.", delimiter);
          System.out.printf("\nTotal number of delimeters= %d ", cntDelimeters);
          String[] lexemes = new String[cntDelimeters + 1];
  
          // parse cntDelimeters+1 lexemes and store in an array
          int right = 0;
          int left = 0;
  
          for (int i = 0; i &lt; cntDelimeters; i++) {
              while (s.charAt(right) != delimiter) {
                  right++;
              }
  
              lexemes[i] = s.substring(left, right);
              right++;
              left = right;
          }
  
          lexemes[cntDelimeters] = s.substring(right, s.length());
  
          // print results for testing
          for (int i = 0; i &lt; lexemes.length; i++) {
              System.out.printf("\n%d : %s", i, lexemes[i]);
          }
      }
  }
  </code></pre>
  
  <p>line 41 is:while (s.charAt(right) != delimiter) {</p>
  
  <p>Some reason string s is only an a</p>
  
  <p>any help is much appreciated.</p>