0

我遇到了一个问题,当我尝试计算科学表达式时,它会编译,但是当我运行它时它会崩溃。我的程序的工作方式是它接收一个包含需要评估的数学表达式的字符串。首先,它被拆分为一个 ArrayList,分别包含数字和运算符。然后搜索允许程序首先使用括号内的表达式的任何括号。然后它调用计算来计算答案。然而,我一直收到的错误是“线程中的异常”AWT-EventQueue-0”java.lang.IndexOutOfBoundsExcepton: Index:0, Size:0”我认为它与我的数组列表的大小有关但是,我无法发现错误,其他人可以帮我弄清楚吗?我认为问题很可能与 splitLabel 和 calculate 方法有关

public ArrayList<String> splitLabel(String val){
    ArrayList<String> label_split = new ArrayList<String>();
    String value = "";
    String op = "";

    for (int i = 0; i< val.length(); i++){
        boolean isDigit = Character.toString(val.charAt(i)).matches("[0123456789.]+");
        boolean isOperator = Character.toString(val.charAt(i)).matches("[+*/^-]+");
        boolean isSin = (Character.toString(val.charAt(i)).equals("s") && Character.toString(val.charAt(i+1)).equals("i") && Character.toString(val.charAt(i+2)).equals("n"));
        boolean isCos = (Character.toString(val.charAt(i)).equals("c") && Character.toString(val.charAt(i+1)).equals("o") && Character.toString(val.charAt(i+2)).equals("s"));
        boolean isTan = (Character.toString(val.charAt(i)).equals("t") && Character.toString(val.charAt(i+1)).equals("a") && Character.toString(val.charAt(i+2)).equals("n"));
        boolean isOpBracket = Character.toString(val.charAt(i)).equals("(");
        boolean isClBracket = Character.toString(val.charAt(i)).equals(")");  

            if (isOperator && !value.equals("")){
                op = Character.toString(val.charAt(i));
                label_split.add(value);
                label_split.add(op);
                op = "";
                value = "";
            } else if (isOperator && value.equals("")){
                if (Character.toString(val.charAt(i)).equals("-")){
                    value = Character.toString(val.charAt(i));
                }
            } else if (isSin){
                label_split.add("sin");
            }else if (isCos){
                label_split.add("cos");
            }else if (isTan){
                label_split.add("tan");
            } else if (isOpBracket && !value.equals("")){
                label_split.add(value);
                label_split.add("(");
                value = "";
            } else if (isOpBracket && value.equals("")){
                label_split.add("(");
            } else if (isClBracket && !value.equals("")){
                label_split.add(value);
                label_split.add(")");
                value = "";
            }
        if (i== val.length()-1 && !value.equals("")){
            label_split.add(value);
        } else if (i== val.length()-1 && Character.toString(val.charAt(i)).equals(")")){
            label_split.add(")");
        }
    } return label_split;
}   
public String bracket(ArrayList<String> label_split){
    ArrayList<Integer> opBra = new ArrayList<Integer>();
    ArrayList<Integer> clBra = new ArrayList<Integer>();
    ArrayList<String> calculation = new ArrayList<String>();
    int counter = 0;
    int counter1 = 0;

    if (label_split.contains("(") && label_split.contains(")")){
        for (int j=0; j<label_split.size(); j++){
            if (label_split.get(j).equals("(")){
                counter = counter + 1;
                opBra.add(j);
            } else if (label_split.get(j).equals(")")){
                counter1 = counter1 + 1;
                clBra.add(j);
            }
        } 
        if (counter1 != counter){
            return "error missing bracket";
        } else {
            for (int j=opBra.size(); j>0; j--){
                int opBraPos = opBra.get(j) + 1; //+1 and -1 so it doesn't include ()
                int clBraPos = clBra.get(opBra.size()-j) - 1;
                opBra.remove(j);
                clBra.remove(opBra.size()-j);

                for(int t = 0; t < (clBraPos - opBraPos); t++){
                    calculation.add(label_split.get(t+opBraPos));
                }

                String value = calculate(calculation);
                label_split.set(j , value);
                calculation.clear();

                for (int n = 0; n < ((clBraPos+1) - opBraPos); n++){
                    label_split.remove(n);
                }
            }
        }
        return calculate(label_split);
    } else{
        return calculate(label_split);
    } 
}
public String calculate(ArrayList<String> calculation){
    double value = 0.0;
    String value1 = "";
    boolean isOperator = calculation.contains("[+*/^-]+");  
    boolean isSin =  calculation.contains("sin"); //...ETC


    for (int i=0; i < calculation.size(); i++){
        if (calculation.get(i).equals("^") && i < calculation.size() && i < 0){
            boolean isDigit1 = calculation.get(i-1).matches("[0123456789.-]+");
            boolean isDigit2 = calculation.get(i+1).matches("[0123456789.-]+");
            if (isDigit1 && isDigit2){
                value = Math.pow(Double.parseDouble(calculation.get(i-1)), Double.parseDouble(calculation.get(i+1)));
                value1 = Double.toString(value);
                calculation.set(i,value1);
                calculation.remove(i-1);
                calculation.remove(i+1);
            }
        }
    }

    for (int a=0; a < calculation.size(); a++){
        if ( (calculation.get(a)).equals("sin") && a < calculation.size() && a < 0){
            boolean isDigit1 = calculation.get(a+1).matches("[0123456789.-]+");
            if (isDigit1){
                value = Math.sin(Double.parseDouble(calculation.get(a+1)));
                value1 = Double.toString(value);
                calculation.set(a,value1);
                calculation.remove(a+1);
            }
        }
    }

    for (int b=0; b < calculation.size(); b++){
        if ( (calculation.get(b)).equals("cos") && b < calculation.size() && b < 0){
            boolean isDigit1 = calculation.get(b+1).matches("[0123456789.-]+");
            if (isDigit1){
                value = Math.cos(Double.parseDouble(calculation.get(b+1)));
                value1 = Double.toString(value);
                calculation.set(b,value1);
                calculation.remove(b+1);
            }
        }
    } // ETC

    return calculation.get(0);
}
4

1 回答 1

0

这不是你的问题,但它肯定看起来是错误的:

calculation.set(i,value1);
calculation.remove(i-1);
calculation.remove(i+1);

问题是这remove(i-1)将导致所有内容的索引从i后面减一。然后remove(i+1)将删除以前在 index 处的元素i+2

然后是这个:

for (int i=0; i < calculation.size(); i++){
    if (calculation.get(i).equals("^") && 
        i < calculation.size() && 
        i < 0) {

想想看。如果i从零开始并递增到size()非负数,那么怎么可能i小于零。如果i永远不小于零,那么if测试怎么可能成功。(你在其他地方重复这个模式。)

但是,真正的问题是您以某种方式调用calculate了一个空列表。

我认为是时候学会使用调试器了......

于 2016-12-05T11:05:13.667 回答