0

Hi I am attempting to create a calculator where the user inputs as many numbers it likes with as many operators. So for example, inputs could be "45+56*78". It is stored as a string. I have created two methods, one method which simply groups the digits together to identify whole numbers and then store them in an arraylist in the correct order with the operators. The other method simply carries out the calculation on the numbers and operators. However, whilst I get no compilation errors, I do get an error when I input a calculation and press on the equal sign button.

The error when I input "3+4" is : Exception in thread "AWI-EventQueue-0" java.lang.IndexOutOfBoundsException: x = 2, Size: 2

I have been looking at my two methods for a while now and still cannot find the error, Can anyone help please? Thank you.

 ActionListener button_eqListener = new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            label2.setText(calculation(splitLabel(label.getText()))); //clicking the equal button calls the methods.
        }
    };  


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("[+*/-]+");

        if (isDigit && !op.equals("")){
            String num = Character.toString(val.charAt(i));
            value = value + num;
            label_split.add(op);
            op = "";
        }else if (isDigit && op.equals("")){
            String num = Character.toString(val.charAt(i));
            value = value + num;
        } else if (isOperator && !value.equals("")){
            label_split.add(value);
            value = "";
            op = Character.toString(val.charAt(i));
        } else if (isOperator && value.equals("")){
            if (Character.toString(val.charAt(i)).equals("-")){
                value = Character.toString(val.charAt(i));
            }
        } else if (i== val.length()-1 && !value.equals("")){
            label_split.add(value);
        }
    } return label_split;
}

public String calculation(ArrayList<String> label_split){
    Double value = 0.0;
    for (int i=0; i <= label_split.size()-1; i++){
        boolean isDigit = label_split.get(i).matches("[0123456789.-]+");
        boolean isOperator = label_split.get(i).matches("[+*/-]+");         
        if (isDigit && value == 0){
            value = Double.parseDouble(label_split.get(i));
        } else if (isOperator) {
            if (label_split.get(i).equals("+")){
                boolean isDigit1 = label_split.get(i+1).matches("[0123456789.-]+");
                if (isDigit1){
                    value = value + Double.parseDouble(label_split.get(i+1));
                }
            } else if (label_split.get(i).equals("-")){
                boolean isDigit1 = label_split.get(i+1).matches("[0123456789.-]+");
                if (isDigit1){
                    value = value - Double.parseDouble(label_split.get(i+1));
                }
            } else if (label_split.get(i).equals("*")){
                boolean isDigit1 = label_split.get(i+1).matches("[0123456789.-]+");
                if (isDigit1){
                    value = value * Double.parseDouble(label_split.get(i+1));
                }
            } else if (label_split.get(i).equals("/")){
                boolean isDigit1 = label_split.get(i+1).matches("[0123456789.-]+");
                if (isDigit1){
                    value = value / Double.parseDouble(label_split.get(i+1));
                }
            } 
        }
    }
    if ((Double.toString(value)).length()>= 11){
        String error = "ERROR";
        return error;
    } else {
        return Double.toString(value);
    }
}
assassinweed2
  • 71
  • 3
  • 9
  • 2
    `value = value + Double.parseDouble(label_split.get(i+1));`throws the error because of label_split.get(i+1). try without the +1. – XtremeBaumer Dec 02 '16 at 15:30
  • @xreme you know I could do the slightest change in the world make your comment a proper answer – Tilak Maddy Dec 02 '16 at 15:39
  • See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Dec 02 '16 at 15:42

0 Answers0