NumberFormatException and other problems while making a calculator in java -


so i'm making java calculator , walking errors upon clicking function buttons (it compiles fine though). error on 1st line in cmd numberformat exception. after searching figured out somewhere in code trying double out of empty string (basically i'm trying parse double label text). i'm guessing it's part:

// if statement puts labels text first or second number     if(firstnumber)     {         number1 = double.parsedouble(label.gettext().trim());     } else {         number2 = double.parsedouble(label.gettext().trim());     } 

basically problems when push / * + or - button numberformatexeption: input string "/" etc. can't quite figure out how have fix error (i'm still (somewhat) beginner in java). further down line of errors (there quite alot) whole lot of errors didn't understand such eventdispatchthread, eventqueue , many others. couldn't find explanation on level of experience either i'm asking here.

the numeric buttons work fine.

at run: [http://gyazo.com/71cb4dde449ccf7ece44017388a71a0f]

putting in numbers: [http://gyazo.com/5c7ab6c54ac6da180845c66866d66f8f]

all other buttons give errors in cmd.

here's code (the spacing might messed in parts):

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; // import nimbus import javax.swing.uimanager.*;  public class calculator extends jframe implements actionlistener {     private jpanel bottom = new jpanel(); private jpanel top = new jpanel();     private jlabel label = new jlabel(" ");     private jbutton[] buttons = new jbutton[16];     // booleans calculator functions     boolean add = false, substract = false, devide = false, multiply = false, firstnumber = true;     // numbers calculated     double number1, number2;      public calculator()     {         setlayout(new boxlayout(getcontentpane(), boxlayout.page_axis));          // bottom panel         bottom.setpreferredsize(new dimension(100,100));         bottom.setlayout(new borderlayout());          //add bottom panel frame         add(bottom);          // top panel         top.setpreferredsize(new dimension(300,400));         top.setlayout(new gridlayout(4,4,3,3));         top.setbackground(color.black);         //dont add top panel frame: want top on bottom          // add top panel bottom panel         bottom.add(top);          // label         label.setfont(new font("courier", font.plain, 20));         label.setbackground(color.black);         label.setforeground(color.white);         label.sethorizontalalignment(swingconstants.right); // text right-aligned         label.setopaque(true);          // add label bottom panel         bottom.add(label, borderlayout.north);          // creating buttons         for(int = 0; < buttons.length; i++)         {             buttons[i] = new jbutton("789/456*123+c0=-".substring(i, i+1));             buttons[i].addactionlistener(this);            // add them top panel            top.add(buttons[i]);         }          // nimbus         try {             (lookandfeelinfo info : uimanager.getinstalledlookandfeels()) {                 if ("nimbus".equals(info.getname())) {                     uimanager.setlookandfeel(info.getclassname());                     break;                 }             }         } catch (exception e) {            // if nimbus not available, can set gui , feel.            //set default somehow o.o         }          // frame setters         settitle("calculator");         setsize(400,400);         setvisible(true);         setresizable(false);         setdefaultcloseoperation(exit_on_close);     }      public void clear()     {         label.settext(" ");     }      @override     public void actionperformed(actionevent e)     {         //if's function buttons         if(e.getsource() == buttons[3])         {            // devide            devide = true;             substract = false;            add = false;            multiply = false;             firstnumber = false;            clear();             label.settext("/");         }          if(e.getsource() == buttons[7])         {            // multiply            multiply = true;             substract = false;            devide = false;            add = false;             firstnumber = false;            clear();             label.settext("*");         }          if(e.getsource() == buttons[11])         {            // add            add = true;             substract = false;            devide = false;            multiply = false;             firstnumber = false;            clear();             label.settext("+");         }          if(e.getsource() == buttons[12])         {            // clear            label.settext("0");            number1 = 0.00;            number2 = 0.00;             add = false;            substract = false;            devide = false;            multiply = false;             firstnumber = true;            clear();         }          if(e.getsource() == buttons[15])         {            // substract            substract = true;             add = false;            devide = false;            multiply = false;             firstnumber = false;            clear();             label.settext("-");         }          // loops add numbers on buttons label         for(int = 0; < 3; i++)         {            if(e.getsource() == buttons[i])            {                if(label.gettext() == "0")                {                    label.settext("");                    label.settext(label.gettext() + buttons[i].gettext());                } else {                    label.settext(label.gettext() + buttons[i].gettext());                }            }         }          for(int = 4; < 7; i++)         {            if(e.getsource() == buttons[i])            {                if(label.gettext() == "0")                {                    label.settext("");                    label.settext(label.gettext() + buttons[i].gettext());                } else {                    label.settext(label.gettext() + buttons[i].gettext());                }            }         }          for(int = 8; < 11; i++)         {            if(e.getsource() == buttons[i])            {                if(label.gettext() == "0")                {                 label.settext("");                 label.settext(label.gettext() + buttons[i].gettext());                } else {                 label.settext(label.gettext() + buttons[i].gettext());                }            }         }          for(int = 13; < 14; i++)         {            if(e.getsource() == buttons[i])            {              if(label.gettext() == "0")              {                 label.settext("");                 label.settext(label.gettext() + buttons[i].gettext());              } else {                 label.settext(label.gettext() + buttons[i].gettext());              }          }      }       // if statement puts labels text first or second number      if(firstnumber)      {         number1 = double.parsedouble(label.gettext().trim());      } else {         number2 = double.parsedouble(label.gettext().trim());      }       // calculation      if(e.getsource() == buttons[14])      {         // calculate         if(devide){number1 = ((double)(number1) / (double)(number2)); }         if(multiply){number1 = ((double)(number1) * (double)(number2)); }         if(add){number1 = ((double)(number1) + (double)(number2)); }         if(substract){number1 = ((double)(number1) - (double)(number2)); }         label.settext(double.tostring(number1));      }   }   public static void main(string[] args)  {     new calculator();  } } 

lastly, calculations made calculator incorrect. can't wrap head around what's causing that. please bear in mind i'm beginner @ java , first question on stackoverflow. in advance helping me, whoever :)

update: fixed errors putting code follows:

if(e.getsource() == buttons[15])     {         // substract         substract = true;          add = false;         devide = false;         multiply = false;          firstnumber = false;         isnumberkey = false;          if(isnumberkey)         {             if(firstnumber)             {                 label.settext(label.gettext().replace("/",""));                 label.settext(label.gettext().replace("*",""));                 label.settext(label.gettext().replace("+",""));                 label.settext(label.gettext().replace("-",""));                 number1 = double.parsedouble(label.gettext().trim());             } else {                 label.settext(label.gettext().replace("/",""));                 label.settext(label.gettext().replace("*",""));                 label.settext(label.gettext().replace("+",""));                 label.settext(label.gettext().replace("-",""));                 number2 = double.parsedouble(label.gettext().trim());             }         }          clear();          label.settext("-");     } 

all need fix calculations... everyone!

cannot delve totaly logic there, here's hints:

first, don't have guess error is. in case of uncaught exception here, thread produced print it's stack trace console. looks this:

exception in thread "awt-eventqueue-0" java.lang.numberformatexception: input string: "/" @ sun.misc.floatingdecimal.readjavaformatstring(floatingdecimal.java:1222) @ java.lang.double.parsedouble(double.java:510) @ mypackage.calculator.actionperformed(calculator.java:229) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:1995) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2318) @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:387) @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:242) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:236) @ java.awt.component.processmouseevent(component.java:6297) 

from here see exception happened in thread named awt-eventqueue-0, swing's standard event dispatching thread (all gui applications handles events in single thread). thing search in stack trace find line in code thrown it. here it's line:

at mypackage.calculator.actionperformed(calculator.java:229)

in source, line:

number2 = double.parsedouble(label.gettext().trim());

, guessed right. can add boolean there set if number button hit, , change part of code like:

if( isnumber){    if(firstnumber)      {          number1 =     double.parsedouble(label.gettext().trim());      } else {          number2 = double.parsedouble(label.gettext().trim());      }  }

apart that, instead of using multiple boolean flags operation, use java enum type

enum operation {devide, substract, add, multiply};

your code more readable , "java" styled. starting point, go ahead!


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -