我写这段代码是为了递归地计算一个表达式(我还没有完成,只是在括号和括号上工作),我刚刚完成了乘法/除法/加法/减法的递归。我得到一个StringOutOfBoundsException为String sub1 = s.substring(0,i);
,有什么想法吗?我放了一些print语句来检查I的值,它从来没有作为一个字符串索引不可能的值出现,那么我的问题是什么呢?
public float evaluate() {
String s = expr;
float answer = 0;
//one single variable or just a number
if(s.contains("+") == false && s.contains("-") == false && s.contains("*") == false && s.contains("/") == false && s.contains("[") == false &&s.contains("]") == false && s.contains("(") == false && s.contains(")") == false){
if(scalars.size() == 0){
answer = Float.parseFloat(s);
return answer;
}
answer = this.scalars.get(0).value;
System.out.println("one var/number loop");
return answer;
}
//no parentheses/brackets
if(s.contains("(") == false && s.contains(")") == false && s.contains("[") == false && s.contains("]") == false && (s.contains("+") == true || s.contains("-") == true || s.contains("*") == true || s.contains("/") == true)){
answer = evalNoPB(s);
System.out.println("no parens loop");
return answer;
}
//make compiler happy
System.out.println("no loop");
return 0;
}
private float evalNoPB(String s){
float tempAns = 0;
if(s.contains("(") == false && s.contains(")") == false && s.contains("[") == false && s.contains("]") == false){
int i;
for(i=s.length()-1; i>=0; i--){
if(s.charAt(i) == '+' || s.charAt(i) == '-'){
System.out.println(i);
break; // keep value of i for substrings
}
} if (i<0) { // for loop went through and did not find + or -
for(i=s.length()-1; i>=0; i--){
if(s.charAt(i) == '*' || s.charAt(i) == '/'){
System.out.println(i);
break; // keep value of i for substrings
}
}
}
String sub1 = s.substring(0,i);
String sub2 = s.substring(i+1, s.length());
if(s.charAt(i) == '+'){
tempAns = evalNoPB(sub1) + evalNoPB(sub2);
} else if(s.charAt(i) == '-'){
tempAns = evalNoPB(sub1) - evalNoPB(sub2);
}else if(s.charAt(i) == '*'){
tempAns = evalNoPB(sub1) * evalNoPB(sub2);
}else if (s.charAt(i) == '/'){
float divisorCheck = evalNoPB(sub2);
if(divisorCheck!= 0){
tempAns = evalNoPB(sub1) / evalNoPB(sub2);
}else { // cannot divide by 0
throw new IllegalArgumentException("cannot divide by 0");
}
}
}
return tempAns;
}
测试Enter the expression, or hit return to quit => 3*3
1 (this is the i value)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String
index out of range: -1
at java.lang.String.substring(Unknown Source)
at apps.Expression.evalNoPB(Expression.java:306)
at apps.Expression.evalNoPB(Expression.java:314)
at apps.Expression.evaluate(Expression.java:280)
at apps.Evaluator.main(Evaluator.java:36)
for(i=s.length()-1; i>=0; i--){
if(s.charAt(i) == '*' || s.charAt(i) == '/'){
System.out.println(i);
break; // keep value of i for substrings
}
}
在此代码段中,变量i最终变为-1并继续执行。即使实际的设计应该改变,对于这一步,只需在for语句之后添加另一个if语句就可以解决这个问题。
if (i < 0) { // for loop went through and did not find + or -
for (i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == '*' || s.charAt(i) == '/') {
System.out.println(i);
break; // keep value of i for substrings
}
}
}
if(i < 0)
return tempAns;
String sub1 = s.substring(0, i);
但只有这种情况下,你会有其他的问题。