I'm building this calculator app with two textviews (one for the formula and one for the result). Now the result after calculations is stored in a variable called result which is left empty by default. But when I run it, after the function executes, everything works but the result variable still remains an empty string. Please help me.
ScientificCalculator.java
int prevAnswer = 0;
TextView formulascreen;
TextView resultscreen;
String formuladisplay = "";
String resultdisplay = "";
String result = "";
String operator;
public void onClickEquals(View view) {
Toast toast = Toast.makeText(getContext(), "Equals to: " + String.valueOf(result), Toast.LENGTH_SHORT);
toast.show();
if (operator == "+" || operator == "-" || operator == "x" || operator == "÷") {
getResult();
updateresultdisplay();
}
}
private boolean getResult() {
if (operator == "") return false;
String[] operation = formuladisplay.split(Pattern.quote(operator));
if (operation.length < 2) return false;
result = String.valueOf(res.format(simpleOp(operation[0], operation[1], operator)));
return true;
}
public double simpleOp(String a, String b, String op) {
switch (op) {
case "+":
return Double.valueOf(a) + Double.valueOf(b);
case "-":
return Double.valueOf(a) - Double.valueOf(b);
case "x":
return Double.valueOf(a) * Double.valueOf(b);
case "÷":
return Double.valueOf(a) / Double.valueOf(b);
default:
return -1;
}
}