- , *, / operators not working on my basic calculator (javafx)

Viewed 38

I'm working on this basic calculator project using javafx and fxml on eclipse. + operators work perfectly fine, but the /, -, * always gives an output of zero. Can't figure out the problem

Here is the class that process the input :

public class MainProcess {
    @FXML
    public Label result;
   
    private long num1 = 0;
    private long num2 = 0;
    private String operator = "";
    
    private boolean start = true;
    private Calculation model = new Calculation();
    
    @FXML
    public void processNumbers(ActionEvent event) {
        if (start) 
        {
            result.setText("");
            start = false;
        }
        
        String value = ((Button)event.getSource()).getText();
        result.setText(result.getText() + value);
    }
    
    @FXML
    public void processOperators(ActionEvent event) 
    {
        
        String value = ((Button)event.getSource()).getText();
        
        if (!value.equals("=")) 
        {
            if (!operator.isEmpty()) 
                return;
            
            operator = value;
            num1 = Long.parseLong(result.getText());
            result.setText("");
            return;
            }
        else 
        {
            if (operator.isEmpty()) 
                return;
            num2 = Long.parseLong(result.getText());
            
            long output = model.calculate(num1, num2, operator);
            result.setText(String.valueOf(output));
            operator = "";
            start = true;
            
        }
    }
    
    @FXML
    public void Ac(ActionEvent event) {
        operator = "";
        start = true;
        result.setText(""); 
    }

}

Here is the class that is doing the arithmetic :

public class Calculation {

    public long calculate(long num1, long num2, String operator) 
    {
        switch (operator) 
        {
        case "+":
            return num1+num2;
        case "-":
            return num1-num2;
        case "*":
            return num1*num2;
        case "/":
            if(num2==0)
                return 0;
            return num1/num2;
        default:
            return 0 ;
        }
    
}

    
}

Here is the fxml :

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="537.0" prefWidth="298.0" style="-fx-background-color: rgba(0,0,0,0);" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainProcess">
   <children>
      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: #1C1C1C; -fx-background-radius: 30;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <Label fx:id="result" alignment="BOTTOM_RIGHT" prefHeight="197.0" prefWidth="361.0" style="-fx-background-color: #af21611;" text="0" textFill="WHITE">
               <VBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </VBox.margin>
               <font>
                  <Font name="Arial" size="48.0" />
               </font>
            </Label>
            <GridPane alignment="CENTER" hgap="10.0" vgap="10.0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" />
                  <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" />
                  <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" />
                <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints maxHeight="-Infinity" minHeight="10.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="-Infinity" minHeight="10.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="-Infinity" minHeight="10.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="-Infinity" minHeight="10.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="-Infinity" minHeight="10.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="-Infinity" minHeight="10.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Button mnemonicParsing="false" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #D4D4D2;" text="+/-" GridPane.columnIndex="1">
                     <font>
                        <Font name="Arial" size="19.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #D4D4D2;" text="\%" GridPane.columnIndex="2">
                     <font>
                        <Font name="Arial" size="19.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processOperators" prefHeight="55.0" prefWidth="55.0" style="-fx-background-color: FF9500; -fx-background-radius: 30;" text="/" textFill="WHITE" GridPane.columnIndex="3">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processNumbers" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #505050;" text="4" textFill="WHITE" GridPane.rowIndex="2">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processNumbers" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #505050;" text="7" textFill="WHITE" GridPane.rowIndex="1">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processNumbers" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #505050;" text="8" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="1">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processNumbers" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #505050;" text="5" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="2">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processNumbers" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #505050;" text="9" textFill="WHITE" GridPane.columnIndex="2" GridPane.rowIndex="1">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processNumbers" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #505050;" text="6" textFill="WHITE" GridPane.columnIndex="2" GridPane.rowIndex="2">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processOperators" prefHeight="55.0" prefWidth="55.0" style="-fx-background-color: FF9500; -fx-background-radius: 30;" text="x" textFill="WHITE" GridPane.columnIndex="3" GridPane.rowIndex="1">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processOperators" prefHeight="55.0" prefWidth="55.0" style="-fx-background-color: FF9500; -fx-background-radius: 30;" text="−" textFill="WHITE" GridPane.columnIndex="3" GridPane.rowIndex="2">
                     <font>
                        <Font name="Arial" size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processNumbers" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #505050;" text="1" textFill="WHITE" GridPane.rowIndex="3">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processNumbers" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #505050;" text="2" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="3">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processNumbers" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #505050;" text="3" textFill="WHITE" GridPane.columnIndex="2" GridPane.rowIndex="3">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processOperators" prefHeight="55.0" prefWidth="55.0" style="-fx-background-color: FF9500; -fx-background-radius: 30;" text="+" textFill="WHITE" GridPane.columnIndex="3" GridPane.rowIndex="3">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processNumbers" prefHeight="57.0" prefWidth="110.0" style="-fx-background-radius: 30; -fx-background-color: #505050;" text="0" textFill="WHITE" GridPane.columnSpan="2" GridPane.rowIndex="4">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #505050;" text="," textFill="WHITE" GridPane.columnIndex="2" GridPane.rowIndex="4">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#processOperators" prefHeight="55.0" prefWidth="55.0" style="-fx-background-color: FF9500; -fx-background-radius: 30;" text="=" textFill="WHITE" GridPane.columnIndex="3" GridPane.rowIndex="4">
                     <font>
                        <Font size="27.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#Ac" prefHeight="55.0" prefWidth="55.0" style="-fx-background-radius: 30; -fx-background-color: #D4D4D2;" text="AC">
                     <font>
                        <Font name="Arial" size="19.0" />
                     </font>
                  </Button>
               </children>
            </GridPane>
         </children>
      </VBox>
   </children>
</AnchorPane>

Note that in my fxml, each button is associated to the right method, so the problem must come from the javafx code

** Sorry if the way of asking the question is not right, Ill take any good advice

Thanks!

0 Answers
Related