Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6226439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:01:51+00:00 2026-05-24T09:01:51+00:00

Trying to create action listeners to perform multiplication tasks. It seems to work, however

  • 0

Trying to create action listeners to perform multiplication tasks. It seems to work, however it seems to ignore my last digit entered. I keep thinking I need a second variable to keep track of last command so that when the equal button is pressed it adds the current digit to the previous ones. But then I wouldn’t I have to perform the task if the equal button is pressed?

class ButtonListener implements ActionListener {        
    public void actionPerformed(ActionEvent e) {
        String numbers = e.getActionCommand();
        if (begin) {
            textField.setText(numbers);
            begin = false;
        }

        else if (numbers.equals("C")) {
            textField.setText("0.0");
            action.reset();
        }

        else 
            textField.setText(textField.getText() + numbers);
    }
}

class OperatorListener implements ActionListener {  

    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        String text = textField.getText();

        if (begin) {
            textField.setText("0.0");
            action.setTotal("0.0");
        }
        else {
            if (command.equals("+")) {
                action.add(text);
                begin = true;
            }
            else if (command.equals("=")) {
                textField.setText(text);
                System.out.println(action.getTotal());
            }
            textField.setText(action.getTotal());
        }
    }
}

Some explanation of variable. begin simply checks if the current state of the JTextField is blank or not. action is simply the method to “add”, it has other calls I want it to do.

Any suggestions on how to keep track of last command or get around so it doesn’t ignore the last digit? These tasks are similar to those of a calculator.


EDIT:
For those interested, here is what I ended up with.

class ButtonListener implements ActionListener {        
    public void actionPerformed(ActionEvent e) {
        String numbers = e.getActionCommand();

        // check if a number has been pressed
        if (begin) {
            textField.setText(numbers);
            begin = false;
        }

        // if clear is checked
        else if (numbers.equals("C")) {
            action.reset();
            begin = true;
            operator = "=";
            textField.setText("0.0");
        }

        // Once a number is pressed keep adding it to the text field until an operator is pressed

        else 
            textField.setText(textField.getText() + numbers);
    }
}

/**
 * Action listener for Operators
 */

class OperatorListener implements ActionListener {  

    public void actionPerformed(ActionEvent e) {
        // String command = e.getActionCommand();

        // if nothing has been pressed
        if (begin) {
            textField.setText("0.0");
            begin = false;

        // else begin performing operation pressed
        } else {
            begin = true; 
            String text = textField.getText();

            // right away add the value of the text field
            if (operator.equals("=")) {
                action.setTotal(text);
            } 
            else if (operator.equals("+")) {
                action.add(text);
            } 
            // and so on for all the other operators.

            textField.setText("" + action.getTotal());

            // now capture the operator pressed.
            operator = e.getActionCommand();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:01:51+00:00Added an answer on May 24, 2026 at 9:01 am

    Side note: In general it’s easier to make a separate listener for each button, rather than trying to share a listener. When you make one listener, then you’re first funnelling several buttons to one function, and then requiring that function to separate them out. Why not just keep them separate to begin with? If there’s common code, call a common function.

    But to your question: If all operators have equal precedence, then just save the operator. As in:

    class Calculator
    {
      // Well, this would probably better be an enum, but whatever.
      public char operator=' ';
      public float lastNum;
    
      public void calc()
      {
        try
        {
          double currNum=Double.parseDouble(numfield.getText());
          if (operator==' ')
            lastNum=currNum;
          else if (operator=='+')
            lastNum+=currNum;
          else if (operator=='-')
            lastNum-=currNum;
          else if (operator=='*')
            lastNum*=currNum;
          else if (operator=='/')
            lastNum/=currNum;
        }
        catch (NumberFormatException panic)
        {
          ... whatever error handling ...
        }
      }
    }
    class OperatorListener implements ActionListener
    {
       Calculator calc;
       public OpeatorListener(Calculator calc)
       {
         this.calc=calc;
       }
       public abstract void actionPerformed(ActionEvent e);
    }
    class PlusListener extends OperatorListener
    {
      public void actionPeformed(ActionEvent e)
      {
        calc.calc();
        calc.operator='+';
      }
    }
    class MinusListener extends OperatorListener
    {
      public void actionPerformed(ActionEvent e)
      {
        calc.calc();
        calc.operator='-';
      }
    }
    class EqualListener extends OperatorListener
    {
      public void actionPerformed(ActionEvent e)
      {
        calc.calc();
      }
    }
    

    This code gives equal precedence to all operators, e.g. “2+3*5=” would give 25.

    If you want multiplication and division to have higher precedence, e.g. “2+3*5=” gives 17, you’d need to create a stack and hold operators and temporary intermediate values and some other complexity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an extension method for the generic delegate Action<T> to
I'm trying to create a record within a join table from the action of
I'm trying to create a record within a join table from the action of
I am trying to create an ActiveRecord object via a JSON request. However the
Trying to create the simplest EJB using NetBeans 7.0 and EJB 3 in Action
I am trying to create a program to perform a simple task and produce
I'm trying to create a custom action for my Wix install, and it's just
I'm trying to create a link with Url.Action , which ends with a #something
I am trying to create controller actions which will return either JSON or partial
I'm trying to make it so when a model is created via created action

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.