我编写了这段代码来递归计算一个表达式(我没有完成,只是按照括号和圆括号的方式工作),我刚刚完成了乘法/除法/加法/减法的递归。我得到了String sub1 = s.substring(0,i);
的StringOutOfBoundsException,有什么想法吗?我把一些打印语句放入检查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)
答案 0 :(得分:0)
通过String方法抛出,以指示索引是负数还是大于字符串的大小。对于某些方法(如charAt方法),当索引等于字符串的大小时,也会抛出此异常。
Here's an example:
String s = "abc";
char c = s.charAt(3);
答案 1 :(得分:0)
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);
但只有这种情况,你还会遇到其他问题。