swing - Java Calculator Operator Errors -


i build calculator java swing library. every thing else works except multiplication , division operators in actionevent loop. other operators work completely.

this error occurs: have tried try statement on part of code

calculator:

enter image description here

calculator multiplication error:

  1. first enter number

  2. then press operator suppose clear textfield - error occurs @ step

  3. then enter second number

  4. then press = button output answer

enter number enter image description here enter image description here enter image description here

picture of error:

if(e.equals("*")) {         fnum = txt.gettext();         logic.settotal(fnum);         op = "*";         txt.settext(""); // error occurs here, textfield isn't cleared         joptionpane.showmessagedialog(null, fnum); //messagebox see if fnum contains string textfield } if(e.equals("/")) {         fnum = txt.gettext();         op = "/";         txt.settext(""); } 

actionevent loop/function:

public void actionperformed(actionevent ea) {     else if(op.equals("*"))     {         logic.settotal(fnum);         logic.multiplication(snum);         total1 = logic.total;     }     else if(op.equals("/"))     {         logic.settotal(fnum);         logic.divide(snum);         total1 = logic.total;     }     txt.settext(""+total1); } 

logic inner class

inner class:

public class inner extends calculators{     public double total;     public inner()     {         total = 0;     }     public void settotal(string n)     {         total = converttonumber(n);      }     public void divide(string n)     {         total /= converttonumber(n);     }     public void multiplication(string n)     {         total *=converttonumber(n);     } } 

if confused please ask more code because can't include of code.

code if want try out yourself

you @ first creating buttons this:

    ...     jbutton plus = new jbutton("+");     jbutton multiplication = new jbutton("*");     jbutton divide = new jbutton("/");     jbutton minus = new jbutton("-");     ... 

and adding this action listener. lines missing:

    ...     plus.addactionlistener(this);     // missing: multiplication.addactionlistener(this);     // missing: divide.addactionlistener(this);     minus.addactionlistener(this);     ... 

how found bug:

  1. downloaded code, compiled, etc.
  2. ran code, tried addition, multiplication, etc. (checking behaviour of application). kind of black box testing.
  3. looked differences between addition , multiplication analyzing code. related white box testing.
  4. i saw, joptionpane.showmessagedialog(null, fnum); should called - has not been called. sat break point (in eclipse) debugging.
  5. when realized, actionperformed method has not been called @ i, searched lines of code, registering actionlisteners.

besides that: recommend refactor code. benefit rethinking structure of code. better readability, code easier maintain , new features can implemented faster.

i recommend to:

  • reduce visibility of fields. make fields private, can find references them.
  • avoid repetitions (called don't repead yourself technique). example: instead of calling addactionlistener each button make collection of buttons (i.e. arraylist<jbutton> , use for loop call addactionlistener each 1 of them.
  • also avoid duplicated code fragments, defining more, shorter methods
  • consider deleting class calculators , put code directly methods of inner.
  • find more meaningful name inner. maybe intermediateresult or similar.
  • create separate actionlistener instance each button. cost little bit of performance (not noticable humans), avoid long if-chains
  • post code on code review (in stackexchange network) getting more , new ideas

Comments