1

I created a calculator using Java swing.Frame creation and button arrangements were all set.I used ActionListener interface.And the program were execute successfully,when i performed a single operation along with two operands(eg:5+2). Working is success so far.But when I try to do multiple operations at same time with more than two operands(eg:10+2-5),it gives me 0.0 as output everytime.But I need my calculator to perform multiple operations at the same time .And should also follow the BODMAS rule.

I declared a float array named result and a char array named operation,as global variables.

public Calculator implements ActionListener{
float[] result=new float[10];
char[] operation=new char[10];
int i=0;
isOperationClicked=false;

Here is one example of operation button(like +,-,/,*) ,defined inside the actionPerformed function along with else if ladder.

else if(e.getSource()==plusbutton) {
            isOperationClicked=true;
            String oldValue=displaylabel.getText();
            result=new float[10];
            result[i]=Float.parseFloat(oldValue);
            operation=new char[10];
            operation[i]= '+';
            i++;
            }

Also equaltobotton is defined inside the actionPerformed function.Checking each operation symbols with ‘if’ on a ‘while loop’.Here i put one example.

else if(e.getSource()==equaltobutton) {
            int m=0;
            String finalvalue=displaylabel.getText();
            float finalvalueF=Float.parseFloat(finalvalue);
            result[i]=finalvalueF;
            while(m<i) {
                if(operation[m]=='/') {
                    float res=result[m]/result[m+1];
                    result=removeArrayF(result,m);
                    result[m]=res;
                    operation=removeArrayC(operation,m);
                    i--;m--;
                }
                m++;
            }

For display the result

displaylabel.setText(result[0]+"");

This setText function is also include on the last line of equaltobutton's action

The removearrayF and the removearrayC are two methods defined to remove one element from the array. This is what i do .what are the changes i need to do for the proper output for multiple operations at same time?

Amalshanth
  • 29
  • 1
  • 7
  • 1
    Can you provide a pseudo-code rendering of how the process works? How is the data stored as the user enters the values, i.e., the _operation_ array, and the _result_ array. – Reilas Jul 23 '23 at 21:39
  • 3
    To me, this sounds like you need to remove the UI from the equation and debug your algorithm – MadProgrammer Jul 23 '23 at 21:45
  • It would help if you could edit your question to show a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Old Dog Programmer Jul 23 '23 at 21:49
  • I don't understand why you re-create the `result` array on each operation (`result=new float[10];`), this seems like a bad idea. Personally, I'd use a `StringBuilder` and `append` each value/operand and then, when required, convert that to a `char` array (or simply pop each char off the `StringBuilder`, which ever makes sense for you) – MadProgrammer Jul 23 '23 at 22:55
  • @MadProgrammer can you give an algoritham to fix it!? – Amalshanth Jul 24 '23 at 11:56
  • I get the idea of String builder class. But how do make it possible on this situation – Amalshanth Jul 24 '23 at 12:11

0 Answers0