Please consider the following code:
public JButton math_button[] = new JButton[5];
for (int h = 0; h <math_button.length; h++) {
if(event.getSource()==math_button[h]) {
String button_press = math_button[h].getText();
if(math_button[h].getText().equals("Equals")) {
secondn = Integer.parseInt(math_input.getText());
System.out.println(firstn + " math operator " + secondn + " and "+ math_button[h].getText());
System.out.println(calc.Math(button_press, firstn, secondn));
} else {
firstn = Integer.parseInt(math_input.getText());
//math_input.setText("");
//placeholder = calc.Math(math_button[h].getText(), firstn, secondn);
//int secondn = Integer.parseInt(math_input.getText());
//int result = calc.Math(math_button[h].getText(), firstn, secondn);
//math_input.setText(Integer.toString(firstn));
//math_input.setText(Integer.toString(placeholder));
}
}
}
What is the reason that despite the variable button_press
being set to the name of an array object outside of the second IF (nested) loop, the test condition variable math_button[h].getText()
is always passed to the calc.Math
method?
Is the variable of the button_press
string being overridden by the nested IF statement?