java - Variable doesn't get updated after function processes -
this question has answer here:
- how compare strings in java? 23 answers
i'm building calculator app 2 textviews (one formula , 1 result). result after calculations stored in variable called result left empty default. when run it, after function executes, works result variable still remains empty string. please 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; } }
you have 2 result variables: result
, resultdisplay
. calculation code updates result
, within updateresultdisplay()
use display contents of (unchanging) resultdisplay
.
so need update updateresultdisplay()
to
private void updateresultdisplay() { resultscreen.settext(result); }