user5609181
user5609181

Reputation:

How to get the number value from a textfield

I want to make a calculator. I have an array to store my value in it . I have two text fields (text1 and text2) where text1 refer to array[0] and text2 refer to array[1] .. when I insert number 1 in text1 and insert number 2 in text2 , then I get 3 as output but when I insert number 12 in text1 and I insert number 22 in text2 then I still get output 3 , this means that the calculator add 1+2 but not 12+22 .. How can I get the whole value in text1 and text2 to add them together? I mean I want the output should be 12+22=34 ...thanks

stack class

    int[] array = new int[4];
public enum Operation {
        PLUS
    }

    public void CalculateSign(Operation op) {
        switch (op) {
        case PLUS:
            result = (array[0] + array[1]);
            //array[0] = result;
            System.out.println(array[0]);
            System.out.println(array[1]);
            break;
            }

public void Number(int nbr) {

        array[0] = nbr;
    }

calculator class

TextField text1 = new TextField();
TextField text2 = new TextField();

plusButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

                if (event.getSource() == plusButton) {
                    stack.CalculateSign(Stack.Operation.PLUS);
                    text1.setText(Integer.toString(stack.result));

                }
            }

        });

one.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

                if (event.getSource() == one) {

                    stack.Number(1);                                                                        text1.setText(text1.getText()+String.valueOf(stack.array[0]));

                }
            }

        });

        two.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

                if (event.getSource() == two) {
                    stack.Number(2);
                    text1.setText(text1.getText()+String.valueOf(stack.array[0]));

                }
            }

        });

Upvotes: 0

Views: 1461

Answers (1)

user2390182
user2390182

Reputation: 73460

Your Number(int nbr) method sets the array cells to its parameter value. In your EventHandlers for your two TextFields you have hard-coded Number(1) and Number(2). Ergo, you will always add 1 and 2, regardless of the actal contents of text1 and text2.

Upvotes: 1

Related Questions