Search code examples
javadoublecharacter-arrays

how to add character with double so it works like methamatics java


i have two arraylists one is double name = numbers which store user input(double) and second is character name = operators which store input(+,-,*,/) entered by the user. the code is below. i want that when user enter a values the code should get the numbers value and do the operation entered in operators. please check the code below and tell me how to do it.

List<Double> numbers = new ArrayList<>();
List<Character> operators = new ArrayList<>();
........................

/* after getting input from user the code is now working as below.*/

void result() {
    et1.setText("");
    for (int i = 0; i < handler; i++) {
        double total2 = operators.get(i) + numbers.get(i);
        total3 += total2;
        et1.setText(Double.toString(total3));
    }

}
/*all values are just being add with eachother which are entered by the user.
help me how to solve this issue. */

Solution

  • Basically its not possible with the way you are doing like:

    double total2= operators.get(i)+numbers.get(i);
    

    You would need a switch with operators like:

    switch (operators.get(i)) {
        case '+':
             //do add
             break;
        case '-':
             //subtract
             break
        blah blah...
    }