user7062312
user7062312

Reputation: 115

Using loop to perform actionPerformed( on click button)

I'm trying to implement this actionPerformed method on my calculator program. I'm having trouble creating a loop which will display the buttons and performs calculation when pressed. By the way, I can fix my problem if I do this by one using if statements but I want to make my code cleaner. Here's my code:

     String expr1 = "";

         private static final String[] bText = {"7", "8", "9", "+", "4", "5", "6", "- ", "1", "2", "3", "*", "0", ".", "=", "/", "(", ")", "C", "CE"};
         Button[] buttons = new Button[ bText.length ];

         public void actionPerformed( ActionEvent arg0 ) {

         String input = textField.getText();

         // Trying to get the first 12 items for testing 
         for( int index = 0; index <=12; index++)
         {
             if(arg0.getSource()==buttons[index])
             {

                 if(input.contains("1,2,3,4,5,6,7,8,9,0"))
                 {
                 textField.setText(input + bText[index]);
                 expr1 = expr1 + index;
                 }
                if(input.contains("+-*/")){
                     expr1 = expr1 + bText[index];
              textField.setText("");
                 }
             }

         }    
            for( int i=13; i <=16; i++){
              if(arg0.getSource()==buttons[i]){
             expr1 = expr1 + bText[i];
              textField.setText("");

            }
          }
         /**
           If I do this one by one using if statements I can fix my problem but I want to make my code cleaner. 

          */


          //For CE button
          if (arg0.getSource() == buttons[18]) {
             String s = textField.getText().toString();
             s = s.substring(0, s.length() - 1);
             textField.setText(s);

          }
                else if(arg0.getSource()==buttons[17]) {
                textField.setText("");
                expr1 = "";    
            } 
         // This will calculate expressins. This is a "=" button 
         else if (arg0.getSource() == buttons[19]) {
               System.out.println(expr1);
                textField.setText("" + Integer.toString(calculatorEvaluator.eval(expr1)));
            }


      }

Upvotes: 0

Views: 1311

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

There are lots of ways I can think of approaching this problem, using the Actions API is probably my most preferred, but may be beyond the scope of the requirement here.

I don't think you need to use loops, I don't think they are going to give you any advantage over other approaches.

For example, you could make use of Strings built in regular expression support and simply check the text of the button.

This would, at least, allow you to determine if the text is a number or an operator or some other command, for example...

private final String[] bText = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/", "(", ")", "C", "CE"};
private JButton[] buttons = new JButton[bText.length];

public TestPane() {
    setLayout(new GridLayout(0, 3));
    for (int index = 0; index < bText.length; index++) {
        String text = bText[index];
        JButton btn = new JButton(text);
        buttons[index] = btn;
        btn.addActionListener(this);
        add(btn);
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source instanceof JButton) {
        JButton btn = (JButton)source;
        String text = btn.getText();
        if (text.matches("^[0-9]")) {
            System.out.println(text + " is a number");
        } else if (text.matches("^[=/\\(\\)*=\\-\\+]")) {
            System.out.println(text + " is an operator");
        } else {
            System.out.println(text + " is some other command");
        }
    }
}

Upvotes: 1

Related Questions