Home > Enterprise >  Counting Letters & Words in a Math Equation String
Counting Letters & Words in a Math Equation String

Time:05-06

So, I have written a method named varCount(), shown below, that would count the number of letter variables in a math equation.

// 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 < getEquation().length(); i  ){
            if ((getEquation().charAt(i) >= 65 && getEquation().charAt(i) <= 90)
            || (getEquation().charAt(i) >= 97 && getEquation().charAt(i) <= 122)){
                count  ;
            }
        }
        return count;
    }

This method is made inside a class, so the getter method of the private equation string attribute was used here.

One example of what should result from this was, if you are using the method on a string x 2 = 3, there would be 1 letter variable, which was x, that's counted. After testing, the method returned the expected int value and was functional to that extent.

What I really want to accomplish is, if a user ever puts in a variable like num, then the method should still work like the second comment.

On the first attempt, I thought that, since it would be a word like num, 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:

// 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 < getEquation().length(); i  ){
            if ((getEquation().charAt(i) >= 65 && getEquation().charAt(i) <= 90)
            || (getEquation().charAt(i) >= 97 && getEquation().charAt(i) <= 122)){
                if (i != 0 && (getEquation().charAt(i-1) >= 65 && getEquation().charAt(i-1) <= 90)
                || (getEquation().charAt(i-1) >= 97 && getEquation().charAt(i-1) <= 122)){
                    continue;
                }
                count  ;
            }
        }
        return count;
    }

The issue with this was that the method only resulted in an IndexOutOfBoundsException.

The next attempts were either modifications or replacements of the method body with the regex package, which returned 0 using the Matcher class's groupCount() method, like so:

// 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("[a-zA-Z]");
        Matcher match = alphaRange.matcher(getEquation());
        System.out.println(match.groupCount());
        return match.groupCount();
    }

What am I missing or going wrong about this method?

CodePudding user response:

Okay, I had modified the method to use the regex package's Pattern and Matcher class and managed to get the program to function exactly as intended.

// 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;
        Pattern pattern = Pattern.compile("(a-zA-Z)*");
        Matcher match = pattern.matcher(getEquation());
        if (match.find()){
            count = match.groupCount();
        }
        return count;
    }

I had not realized that the groupCount() method needed to be processed if a match was found, logically. I had also used parentheses instead of open brackets for a more specific range.

What I am reminded about, though, is that there could be times when we need another pair of eyes. Thank you!

  • Related