You'll need to cast Strings
to Integers
to perform math. To display the result, you'll want to convert the Integer result back to a String. For example
String a = "42";
String b = "6";
int addition = Integer.parseInt(a) + Integer.parseInt(b);
display.setText(Integer.toString(addition));
Given that this is a calculator and you know that they can only type numbers, then you should be fine converting these arbitrary Strings to numbers. But, note that in general, Integer.parseInt()
may fail if the input is not a number.
UPDATE: basic blueprint for implementing integer calculator
int currentValue = 0; //at the beginning, the user has not typed anything
//here, I am assuming you have a method that listens for all the button presses, then you could
//call a method like this depending on which button was pressed
public void buttonPressed(String value) {
currentValue += Integer.parseInt(value);
calculatorLabelDisplay.setText(Integer.toString(currentValue));
}
//here, I am assuming there is some button listener for the "clear" button
public void clearButtonPressed() {
currentValue = 0;
}