0

I'm currently creating a calculator where I'm reading the code from a String and adding the operands and operators into an ArrayList. For some reason the system isn't carrying out the operations in my addMinus method. It seems to be that the if-then statements aren't being executed. I'm not understanding why this is. Please assist.

    int counter = 0; //placeholder for the operators
    //this searches to see if there is an operator
    for (int i = 0; i < input.length(); i++) {
        char c = input.charAt(i);
        if (c == '+' || c == '-') {
            operator[counter] = Character.toString(c);
            counter++;
        }
    }
    addMinus(delimiter, operator, counter);
    return null;
}


//Multiplies the numbers
public static String[] multDiv(String[] equation) {
    for (int y = 0; y < equation.length; y++) {//iterates through elements in delimiter
        for (int i = 0; i < equation[y].length(); i++) {//iterates through chars in element string
            if (equation[y].charAt(i) == '*') {
                String[] nums = equation[y].split("\\*");
                equation[y] = String.valueOf(Double.parseDouble(nums[0]) * Double.parseDouble(nums[1]));
            } else if (equation[y].charAt(i) == '/') {
                String[] nums = equation[y].split("/");
                equation[y] = String.valueOf(Double.parseDouble(nums[0]) / Double.parseDouble(nums[1]));
            }
        }
    }
    return equation;
}


/////*****SEARCHES FOR ADDS OR SUBTRACTS THE NUMBERS *****////
public static String[] addMinus(String[] numbers, String[] symbols, int counter) {
    String[] equation = arrayMaker(numbers, symbols, counter);
    Double answ = Double.parseDouble(numbers[0]);
    int x = 0;

    for (int i = 0; i < symbols.length; i++) {
        if (symbols[i] == "- ") {
            answ = -Double.parseDouble(equation[x + 1]);
            System.out.println(" " + answ);
        } else if (symbols[i] == "+ ") {
            System.out.println(symbols[0]);
            answ = +Double.parseDouble(numbers[x + 1]);
            System.out.println(" " + answ);
        }
        x = +2;
    }
    System.out.println(answ);
    return null;
}

Ilya Lysenko
  • 1,772
  • 15
  • 24
Aziziz14
  • 35
  • 3
  • 1
    Post the complete code somewhere like https://repl.it/languages/java10 too. And save it and post back a link. – JGFMK Mar 20 '20 at 19:43

1 Answers1

1

Pay attention to this line of code:

if (symbols[i] == "- ")

You are comparing references here, not values. You need to use equals() method. Same case with else if. And what with extra space between quotes?

Ilya Lysenko
  • 1,772
  • 15
  • 24
Bosko Vaskovic
  • 106
  • 1
  • 6