0

Background

I'm trying to write a program in which the user types in a formula of format

A?B:C?(X-1):D?(Y-3):(Z*3)

and then the code will guide the user through a series of yes/no questions to determine what would be returned under certain conditions.

Problem

I have written a findTrue code which will extract the true part of this kind of formula. I want my yesListener's action to ask another question if the true part of the user's input has more question marks in it. If not, it will return the answer.

        ActionListener yesListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String formula = textArea.getText();
                int i = StringUtils.countMatches(formula, "?");
               if  (i>1) {
                    String newFormula = "";
                    newFormula = findTrue(formula);
                    questionLabel.setText(askQuestion(newFormula));
                }
               else {questionLabel.setText("The formula will return" + findTrue(formula));}
            }};

The first time I run the code it works fine, but the second time it runs the getText() again from the original input. So, I figure the best way is if I can pass the string into the actionPerformed rather than evaluating it inside. However I'm still pretty new to java and I'm struggling to see how I could make this happen.

Community
  • 1
  • 1
Chris A
  • 863
  • 1
  • 10
  • 31
  • Please read [mcve] and enhance your question accordingly. Most likely, this snippet of code isn't enough to tell you what causes your problem. – GhostCat Feb 23 '19 at 10:24
  • I'm not sure what else I can add without being superfluous. The other workings of my code aren't really important, it's just that *as is*, `formula` is constantly getting redefined from the original input each time the action is called. – Chris A Feb 23 '19 at 11:22
  • Having looked at this again, combined with thinking about some things that I saw at work this week (Junior Dev), I think I am trying to do too much code within the GUI itself. I will try to re-write this more logically and come back with a better question if necessary. – Chris A Feb 23 '19 at 11:47

2 Answers2

1

As far as I understand, your ActionListener is created in the scope of some class. It's hard to propose the best way to refactor the code without seeing the whole code of the class.

But to achieve your goal, you could save the original value of getText() in a class instance field, and then update it in each new invokation of the listener:

public class Main {

    private String formula;


    ActionListener yesListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (formula == null) {
                formula = textArea.getText();
            }
            int i = StringUtils.countMatches(formula, "?");
            if (i > 1) {
                formula = findTrue(formula);
                questionLabel.setText(askQuestion(formula));
            } else {
                questionLabel.setText("The formula will return" + findTrue(formula));
            }
        }

    };

Forketyfork
  • 7,416
  • 1
  • 26
  • 33
0

U cant directly pass arguments to anonymous function. But this link might help u to do that how-to-pass-parameters-to-anonymous-class

Tanveer Hasan
  • 334
  • 1
  • 5
  • 15