I am still a beginner but trying to learn so probably doing something extremely silly. My question is about a simple Android calculator I am trying to make but I think the question concerns basic Java which I thought I understood. I get the value from the button that’s pressed then pass it to a method to display the input then set a global variable for the input. The value in the method header is “1” if I press “1” but then gets lost in that method. Maybe I don’t understand how to handle the Java types as I thought I did–don’t know. I had the logic working at one time but had to change how it was displayed and that, of course, screwed up my logic somewhere. Anyway, I will post the passing statements and the receiving method for now. Thanks in advance.
public void initButtons()
{
//register buttons as listeners
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
final Button button5 = (Button) findViewById(R.id.button5);
final Button button6 = (Button) findViewById(R.id.button6);
final Button button7 = (Button) findViewById(R.id.button7);
final Button button8 = (Button) findViewById(R.id.button8);
final Button button9 = (Button) findViewById(R.id.button9);
final Button button10 = (Button) findViewById(R.id.button10);
final Button addButton = (Button) findViewById(R.id.addButton);
final Button subButton = (Button) findViewById(R.id.subButton);
final Button multButton = (Button) findViewById(R.id.multButton);
final Button divButton = (Button) findViewById(R.id.divButton);
final Button calcButton = (Button) findViewById(R.id.calcButton);
final Button clearButton = (Button) findViewById(R.id.clearButton);
//when button is pressed, send num to calc function
button1.setOnClickListener
(new Button.OnClickListener()
{
public void onClick(View v)
{
inputString = button1.getText().toString();
displayCalc(inputString);
//displayCalc(1.0);
}
}
);
private void displayCalc(String curValue)
{
if (hasChanged = false)
{
//display number if reset
String display = curValue;
number1 = Double.parseDouble(display);
}
else
{
// display = display + operator + curValue;
//number2 = Double.parseDouble(curValue);
}
//showDisplay(display);
}
Comparisons in Java are made with double equal signs “==”.
It is considered better form to test for booleans simply by putting them between the parentheses like this :