I've been trying to make a simple calculator using JavaFX and I am very new to this. I have two TextField
s and rest of the buttons are operators like +
, -
, *
, /
. The problem is that whenever I perform a calculation and press the equal, I get zero as a result. The calculator is not performing calculations for any of the operators. Kindly help me with this as I am unable to understand why is this happening. Below is the code of Controller file.
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
public class MainController {
@FXML
private Label result;
@FXML
private TextField num1, num2 ;
private String operator = "";
private int number1, number2;
//private boolean start = true;
private Model model = new Model();
@FXML
public void processNumbers(ActionEvent event) {
number1 = Integer.parseInt(num1.getText());
number2 = Integer.parseInt(num2.getText());
}
@FXML
public void processOperators(ActionEvent event) {
String value = ((Button) event.getSource()).getText();
if (!value.equals("?")) {
if (!operator.isEmpty())
return;
operator = value;
} else {
if (operator.isEmpty())
return;
int output = model.calculate(number1, number2, operator);
result.setText(String.valueOf(output));
operator = "";
//start = true;
}
}
}