[英]Counting Letters & Words in a Math Equation String
So, I have written a method named varCount(), shown below, that would count the number of letter variables in a math equation.因此,我编写了一个名为 varCount() 的方法,如下所示,它将计算数学方程式中字母变量的数量。
// 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.这个方法是在一个class内部做的,所以这里用到了私有方程字符串属性的getter方法。
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.应该由此产生的结果的一个示例是,如果您在字符串x + 2 = 3上使用该方法,则会有 1 个字母变量,即x ,它被计算在内。 After testing, the method returned the expected int value and was functional to that extent.
经过测试,该方法返回了预期的 int 值,并且可以正常运行。
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.我真正想要完成的是,如果用户曾经输入过像num这样的变量,那么该方法仍然应该像第二条评论一样工作。
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:在第一次尝试时,我认为,既然它会是一个像num这样的词,那么,如果前一个字符被算作一个字母,那么整个单词将通过只计算第一个字母来计算,如下所示:
// 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 .问题在于该方法只会导致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:接下来的尝试是使用正则表达式package 修改或替换方法主体,它使用 Matcher 类的 groupCount() 方法返回 0,如下所示:
// 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?这种方法我遗漏了什么或出了什么问题?
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.好的,我修改了使用正则表达式包的 Pattern 和 Matcher class 的方法,并设法使程序完全按照预期到达 function。
// 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.我没有意识到如果找到匹配项,逻辑上需要处理 groupCount() 方法。 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!
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.