AnchovyLegend
AnchovyLegend

Reputation: 12538

Calculator program getting user input

For practice, I am trying to create a fully functional calculator. I copied the ActionListener class of my code below. I feel like my this method for getting the users input, storing it as an integer/double does not work well as it is too basic and does not work in all cases. I was wondering if anyone can help me figure out a way to structure my code, get user input for two numbers and a symbol, and basically create a fully function GUI calculator. My problem is getting user input from my JButton, storing as a number that in way like a calculator does, (press 1 twice == 11) and using it for later calculation.

I appreciate any advice in this regard.

private class TheHandler implements ActionListener{
            public void actionPerformed(ActionEvent event){
                while(equals!=event.getSource())
                    if(one==event.getSource()){
                       result.setText("1");
                       num1=1;
                    }
                    else if(two==event.getSource()){
                       result.setText("2");

                    }
                    else if(three==event.getSource()){
                       result.setText("3");
                    }
                    else if(four==event.getSource()){
                       result.setText("4");
                    }
                    else if(five==event.getSource()){
                       result.setText("5");
                    }
                    else if(six==event.getSource()){
                       result.setText("6");
                    }
                    else if(seven==event.getSource()){
                       result.setText("7");
                    }
                    else if(eight==event.getSource()){
                       result.setText("8");
                    }
                    else if(nine==event.getSource()){
                        result.setText("9");
                    }
                    else if(zero==event.getSource()){
                        result.setText("0");
                    }
                }
            }

Upvotes: 1

Views: 1172

Answers (3)

Michael
Michael

Reputation: 35341

You could more cleanly associate each button with a number like so:

private class TheHandler implements ActionListener{
  private Map<Object, String> numbers = new HashMap<Object, String>();
  {
    numbers.put(zero, "0");
    numbers.put(one, "1");
    ...
  }
  public void actionPerformed(ActionEvent event){
    Object source = event.getSource();
    while(equals != source){
      String number = numbers.get(source);
      if (number != null){
        result.setText(number);
      }
    }
  }
}

Upvotes: 1

stefan bachert
stefan bachert

Reputation: 9608

You should remove the while at all.

I would suggest to use numeric vars like

long num = 0;
int digit;

Each event should just set digit

 ...
 else if(nine==event.getSource()){
     digit = 9;
 } else if(zero==event.getSource()){
     digit = 0;
 }
...
num = num * 10 + digit;
...
result.setText(Long.toString(num));

Upvotes: 3

Usman Ismail
Usman Ismail

Reputation: 18669

How about a class member variable to store the current state of the display and use that for set text?

private class TheHandler implements ActionListener{
    String display = "";
    public void actionPerformed(ActionEvent event){
        while(equals!=event.getSource()){
            if(one==event.getSource()){
                display=display+"1";

            }
            ..... 
        }
        result.setText(display);
    }
}

Upvotes: 2

Related Questions