Reputation: 149
Using eclipse, first of all. I'm building an Android calculator. I have all my code, except there's a problem. One of my variables is not being used. My total1 variable is basically the counter for the program. When I add two numbers up and press equals, it always prints "0.0" and that's the value of total1. That means for some reason, it never changed throughout the whole program. I even tested this by using a print statement. I changed the value of total1 and it would print that value I assigned it to. What could be the problem?
public class HelloAndroidActivity extends Activity {
public EditText display;
String display1;
double displayValue;
// Program is printing out total1, as if it weren't maninpulated in the code
double total1 = 0.0;
double total2 = 0.0;
char theOperator;
public String buttonText;
public Button ButtonAdd, ButtonEqual, ButtonMultiply, ButtonDivide, ButtonMinus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
display = (EditText) findViewById(R.id.editText1);
// Could it have something to do with this if statement?
if (display.length() != 0) {
display1 = display.getText().toString();
displayValue = Double.parseDouble(display1);
}
}
//Get the operator of the multiply, divide, subtract, add buttons when they are clicked and then do the following
public void getOperator(String btnText) {
theOperator = btnText.charAt(0);
total1 += displayValue;
display.setText("");
}
// Display a string in the box when a number button is clicked
public void onClick(View v) {
switch (v.getId()) {
case R.id.bOne:
display.append("1");
break;
case R.id.bTwo:
display.append("2");
break;
case R.id.bThree:
display.append("3");
break;
case R.id.bFour:
display.append("4");
break;
case R.id.bFive:
display.append("5");
break;
case R.id.bSix:
display.append("6");
break;
case R.id.bSeven:
display.append("7");
break;
case R.id.bEight:
display.append("8");
break;
case R.id.bNine:
display.append("9");
break;
case R.id.bZero:
display.append("0");
break;
case R.id.bPoint:
display.append(".");
break;
case R.id.bClear:
total2 = 0.0;
display.setText("");
break;
case R.id.bAdd:
buttonText = "+";
ButtonAdd = (Button) findViewById(R.id.bAdd);
ButtonAdd.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bMinus:
buttonText = "-";
ButtonMinus = (Button) findViewById(R.id.bMinus);
ButtonMinus.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bMultiply:
buttonText = "*";
ButtonMultiply = (Button) findViewById(R.id.bMultiply);
ButtonMultiply.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bDivide:
buttonText = "/";
ButtonDivide = (Button) findViewById(R.id.bDivide);
ButtonDivide.setText(buttonText);
getOperator(buttonText);
break;
// Here's the equals button. When I click it it prints out "0.0", the value of total1, instead of adding the total below
case R.id.bEqual:
switch (theOperator) {
case '+':
total2 = total1 + displayValue;
break;
case '-':
total2 = total1 - displayValue;
break;
case '*':
total2 = total1 * displayValue;
break;
case '/':
total2 = total1 / displayValue;
break;
}
display.setText(Double.toString(total2));
total1 = 0.0;
break;
}
}
}
Upvotes: 0
Views: 198
Reputation: 2703
You are trying to add displayValue
in your get operator method but you have not changed this value. You should be paring this value from the display before adding it to total1
It should be like this:
public void getOperator(String btnText){
theOperator = btnText.charAt(0);
total1+=Double.parseDouble(display.getText().ToString());
display.setText("");
}
Or like this if you need to save the current display value:
public void getOperator(String btnText){
theOperator = btnText.charAt(0);
displayValue = Double.parseDouble(display.getText().ToString());
total1+=displayValue;
display.setText("");
}
Upvotes: 1
Reputation: 23169
I see only one location where your total1
variable is modified, and there it has the value of displayValue
added to it. displayValue
is only modified once, in your onCreate
method (called just once per Activity lifecycle) where it is loaded from the contents of the editText1
control (which we don't have the source for).
So in short I'm not surprised total1
is not being modified. It's a matter of checking the whole logic flow of your app.
Upvotes: 1