I made simple calculator for android for sake of learning but its buggy. All number keys just append
respective number to TextView
. Equals button does the job of adding/subtracting/divide/multiply. But the problem is, it can do only one at a time, if I mix plus operation with minus, it will crash. Here is code of equals button performing addition:
equals = (Button) findViewById(R.id.equals);
equals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.getText();
String expression = (result.getText()).toString();
if(expression.contains("+")){
String[] numbers = expression.split("\\+");
String no1 = numbers[0];
String no2 = numbers[1];
Double numb1 = Double.valueOf(no1);
Double numb2 = Double.valueOf(no2);
Double added = numb1+numb2;
theresult = String.valueOf(added);
result.setText(theresult);
}
}
});
as you can see above, another problem arises. It cant handle more than one +
sign, how can I make it work to add x amount of numbers? Sorry I am new to programming, instead of just making it work, I want to learn how it will work so please explain too if possible. Thanks.