I created a project in JavaFx and I had a problem with gettext. The program itself was supposed to become something like a simple calculator with three functions: add, subtract, multiply, read numbers from two TextFields, and perform various operations, depending on the button pressed. However, when you start the program in the answer field appears: "0 + 0 = 0", etc. Here is the Controller class code:

public class Controller { @FXML public TextField txtY; @FXML public TextField txtX; @FXML public TextArea txtAnswer; @FXML public Button btnMult; @FXML public Button btnMinus; @FXML public Button btnPlus; private int B; private int C; private int A; private int y1; private int x1; public void initialize() { String y = txtY.getText(); String x = txtX.getText(); try { y1 = Integer.parseInt(y); } catch (NumberFormatException ignored) { } try { x1 = Integer.parseInt(x); } catch (NumberFormatException ignored) { } C = x1 * y1; A = x1 - y1; B = x1 + y1; } public void MultyAction(ActionEvent actionEvent) { boolean Multy = (C < 0); if (Multy){ try { Stage error = new Stage(); Parent root = FXMLLoader.load(getClass().getResource("edit.fxml")); error.setTitle("Error"); error.setMinWidth(100); error.setMinHeight(200); error.setResizable(false); error.setScene(new Scene(root)); error.initModality(Modality.WINDOW_MODAL); error.initOwner(((Node) actionEvent.getSource()).getScene().getWindow()); error.show(); } catch (IOException e) { e.printStackTrace(); } } else{ txtAnswer.setText(x1 + " * " + y1 + " = " + C); } } public void MinusAction(ActionEvent actionEvent) { boolean Minus = (A < 0); if (Minus){ try { Stage error = new Stage(); Parent root = FXMLLoader.load(getClass().getResource("edit.fxml")); error.setTitle("Error"); error.setMinWidth(100); error.setMinHeight(200); error.setResizable(false); error.setScene(new Scene(root)); error.initModality(Modality.WINDOW_MODAL); error.initOwner(((Node) actionEvent.getSource()).getScene().getWindow()); error.show(); } catch (IOException e) { e.printStackTrace(); } } else{ txtAnswer.setText(x1 + " - " + y1 + " = " + A); } } public void PlusAction(ActionEvent actionEvent) { boolean Plus = (B < 0); if (Plus){ try { Stage error = new Stage(); Parent root = FXMLLoader.load(getClass().getResource("edit.fxml")); error.setTitle("Error"); error.setMinWidth(100); error.setMinHeight(200); error.setResizable(false); error.setScene(new Scene(root)); error.initModality(Modality.WINDOW_MODAL); error.initOwner(((Node) actionEvent.getSource()).getScene().getWindow()); error.show(); } catch (IOException e) { e.printStackTrace(); } } else{ txtAnswer.setText(x1 + " + " + y1 + " = " + B); } } 

What is my mistake and how to fix it?

    1 answer 1

    The problem is that the initialize() method is not called anywhere, therefore the variable values ​​are default, i.e. for int it is 0.

    When performing an operation you need (add, subtract and multiply) to read variables, i.e. call your initialize() method

    • That is, I need to put initialize () in each Аction? - Mike
    • It turns out that so - Andrew Bystrov
    • But the initialize () method cannot be called inside other methods, isn't it? - Mike
    • Wednesday, when calling initialize (), gives an error expression expected and asks to put ";" - Mike
    • Well, put it; after calling the method - Andrew Bystrov