title: Counting Letters & Words in a Math Equation String
body: <p>So, I have written a method named varCount(), shown below, that would count the number of letter variables in a math equation.</p>
  <pre><code>// This method counts the number of variables in the equation
      /* Note: If a variable is a word or a substring with length greater than 1,
         then count it as a whole */
      public int varCount(){
          int count = 0;
          for (int i = 0; i &lt; getEquation().length(); i++){
              if ((getEquation().charAt(i) &gt;= 65 &amp;&amp; getEquation().charAt(i) &lt;= 90)
              || (getEquation().charAt(i) &gt;= 97 &amp;&amp; getEquation().charAt(i) &lt;= 122)){
                  count++;
              }
          }
          return count;
      }
  </code></pre>
  <p>This method is made inside a class, so the getter method of the private equation string attribute was used here.</p>
  <p>One example of what should result from this was, if you are using the method on a string <em>x + 2 = 3</em>, there would be 1 letter variable, which was <em>x</em>, that's counted. After testing, the method returned the expected int value and was functional to that extent.</p>
  <p>What I really want to accomplish is, if a user ever puts in a variable like <em>num</em>, then the method should still work like the second comment.</p>
  <p>On the first attempt, I thought that, since it would be a word like <em>num</em>, then, if the previous character was counted as a letter, then the whole word would be counted by counting just that first letter, like so:</p>
  <pre><code>// This method counts the number of variables in the equation
      /* Note: If a variable is a word or a substring with length greater than 1,
         then count it as a whole */
      public int varCount(){
          int count = 0;
          for (int i = 0; i &lt; getEquation().length(); i++){
              if ((getEquation().charAt(i) &gt;= 65 &amp;&amp; getEquation().charAt(i) &lt;= 90)
              || (getEquation().charAt(i) &gt;= 97 &amp;&amp; getEquation().charAt(i) &lt;= 122)){
                  if (i != 0 &amp;&amp; (getEquation().charAt(i-1) &gt;= 65 &amp;&amp; getEquation().charAt(i-1) &lt;= 90)
                  || (getEquation().charAt(i-1) &gt;= 97 &amp;&amp; getEquation().charAt(i-1) &lt;= 122)){
                      continue;
                  }
                  count++;
              }
          }
          return count;
      }
  </code></pre>
  <p>The issue with this was that the method only resulted in an <em>IndexOutOfBoundsException</em>.</p>
  <p>The next attempts were either modifications or replacements of the method body with the <em>regex</em> package, which returned 0 using the Matcher class's groupCount() method, like so:</p>
  <pre><code>// This method counts the number of variables in the equation
      /* Note: If a variable is a word or a substring with length greater than 1,
         then count it as a whole */
      public int varCount(){
          Pattern alphaRange = Pattern.compile(&quot;[a-zA-Z]&quot;);
          Matcher match = alphaRange.matcher(getEquation());
          System.out.println(match.groupCount());
          return match.groupCount();
      }
  </code></pre>
  <p>What am I missing or going wrong about this method?</p>