0

So im a bit confused on this. my main problem is in the equals action event. How can i have answer be whatever is the result of answertext? Also sorry if my code is messy

No this isent homework, just for fun... And if possible a submission for my final programming project thing you have to do for your degree

public class MainM extends Application
{
    char check;
    double answer;

    TextField answerText=new TextField("");
    Button b0=new Button("0");
    Button b1=new Button("1");
    Button b2=new Button("2");
    Button b3=new Button("3");
    Button b4=new Button("4");
    Button b5=new Button("5");
    Button b6=new Button("6");
    Button b7=new Button("7");
    Button b8=new Button("8");
    Button b9=new Button("9");

    Button equal=new Button("=");
    Button plush=new Button("+");
    Button min=new Button("-");
    Button div=new Button("%");
    Button mul=new Button("*");

    Button clear=new Button("C");
    Button neg=new Button("-/+");
    Button point=new Button(".");

    @Override
    public void start(Stage primaryStage)
    {
        HBox textPane=new HBox();
        GridPane calcPane=new GridPane();
        BorderPane pane=new BorderPane();

        answerText.setEditable(false);
        answerText.setMinSize(290,30);
        b0.setMinSize(70,60);
        b1.setMinSize(70,60);
        b2.setMinSize(70,60);
        b3.setMinSize(70,60);
        b4.setMinSize(70,60);
        b5.setMinSize(70,60);
        b6.setMinSize(70,60);
        b7.setMinSize(70,60);
        b8.setMinSize(70,60);
        b9.setMinSize(70,60);
        min.setMinSize(70,60);
        plush.setMinSize(70,60);
        mul.setMinSize(70,60);
        div.setMinSize(70,60);
        equal.setMinSize(70,60);
        neg.setMinSize(70,60);
        point.setMinSize(70,60);
        clear.setMinSize(70,60);

        b0.setStyle("-fx-font-size:20");
        b1.setStyle("-fx-font-size:20");
        b2.setStyle("-fx-font-size:20");
        b3.setStyle("-fx-font-size:20");
        b4.setStyle("-fx-font-size:20");
        b5.setStyle("-fx-font-size:20");
        b6.setStyle("-fx-font-size:20");
        b7.setStyle("-fx-font-size:20");
        b8.setStyle("-fx-font-size:20");
        b9.setStyle("-fx-font-size:20");
        min.setStyle("-fx-font-size:20");
        point.setStyle("-fx-font-size:20");
        plush.setStyle("-fx-font-size:20");
        mul.setStyle("-fx-font-size:20");
        div.setStyle("-fx-font-size:20");
        neg.setStyle("-fx-font-size:20");
        equal.setStyle("-fx-font-size:20");
        clear.setStyle("-fx-font-size:20");

        textPane.getChildren().add(answerText);

        calcPane.setHgap(5);
        calcPane.setVgap(5);

        calcPane.add(clear,0,0);
        calcPane.add(neg,1,0);
        calcPane.add(point,2,0);
        calcPane.add(plush,3,0);
        calcPane.add(min,3,1);
        calcPane.add(div,3,2);
        calcPane.add(mul,3,3);
        calcPane.add(equal,3,4);

        calcPane.add(b1,0,1);
        calcPane.add(b2,1,1);
        calcPane.add(b3,2,1);
        calcPane.add(b4,0,2);
        calcPane.add(b5,1,2);
        calcPane.add(b6,2,2);
        calcPane.add(b7,0,3);
        calcPane.add(b8,1,3);
        calcPane.add(b9,2,3);
        calcPane.add(b0,1,4);
        calcPane.setPadding(new Insets( 5,0,0,0));

        pane.setTop(textPane);
        pane.setBottom(calcPane);
        pane.setPadding(new Insets(5,5,5,5));

        Scene scene=new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Calculator FX");
        primaryStage.show();

        if(answerText.getText().equals(""))
        {
            disable();
        }
        else
        {
            enable();
        }

        clear.setOnAction(event -> {
            answerText.setText("");
            check='\0';
            disable();
            point.setDisable(false);
        });
        b0.setOnAction(event ->{
            answerText.appendText("0");
            enable();
        });
        b1.setOnAction(event ->{
            answerText.appendText("1");
            enable();
        });
        b2.setOnAction(event ->{
            answerText.appendText("2");
            enable();
        });
        b3.setOnAction(event ->{
            answerText.appendText("3");
            enable();
        });
        b4.setOnAction(event ->{
            answerText.appendText("4");
            enable();
        });
        b5.setOnAction(event ->{
            answerText.appendText("5");
            enable();
        });
        b6.setOnAction(event ->{
            answerText.appendText("6");
            enable();
        });
        b7.setOnAction(event ->{
            answerText.appendText("7");
            enable();
        });
        b8.setOnAction(event ->{
            answerText.appendText("8");
            enable();
        });
        b9.setOnAction(event ->{
            answerText.appendText("9");
            enable();
        });

        point.setOnAction(event ->{
            answerText.appendText(".");
            disable();
            point.setDisable(true);
        });
        plush.setOnAction(event ->{
            answerText.appendText("+");
            disable();
            point.setDisable(false);
        });
        min.setOnAction(event ->{
            answerText.appendText("-");
            disable();
            point.setDisable(false);
        });
        mul.setOnAction(event ->{
            answerText.appendText("*");
            disable();
            point.setDisable(false);
        });
        div.setOnAction(event ->{
            answerText.appendText("/");
            disable();
            point.setDisable(false);
        });
        equal.setOnAction(event ->{

            //answer=Double.valueOf(answerText.getText());
            //answer=answerText.getText();
            System.out.println(answer);
            //point.setDisable(false);
        });

    }

    public void enable()
    {
        equal.setDisable(false);
        plush.setDisable(false);
        mul.setDisable(false);
        div.setDisable(false);
        min.setDisable(false);
    }
    public void disable()
    {
        equal.setDisable(true);
        plush.setDisable(true);
        mul.setDisable(true);
        div.setDisable(true);
        min.setDisable(true);
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}
mono832
  • 13
  • 4
  • 1
    You need a parser to evaluate the expression: https://stackoverflow.com/questions/3422673/how-to-evaluate-a-math-expression-given-in-string-form – Alex R Jun 06 '20 at 16:54
  • If this is to be submitted for your degree, I'd require you to use arrays for your buttons. – NomadMaker Jun 06 '20 at 23:18

1 Answers1

0

I tried this. seems good. Just needs a bit more cleaning.

Thank you Alex R for the link. The answer i found was the best one since i dident need to download anything and used the built in resources of java

if(answerText.getText().equals("2+2"))
            {
                answerText.setText("|><=>  *Fish*");
            }
            else
            {
                //JESUS
                ScriptEngineManager mgr = new ScriptEngineManager();
                ScriptEngine engine = mgr.getEngineByName("JavaScript");
                String foo = answerText.getText();
                try {
                    System.out.println(engine.eval(answerText.getText()));
                    answerText.setText(engine.eval(answerText.getText()).toString());
                    //answer=Double.valueOf(engine.eval(foo));
                } catch (ScriptException e) {
                    e.printStackTrace();
                }
mono832
  • 13
  • 4