java - NumberFormatException when trying to parse Strings from .getText(); -
exception in thread "awt-eventqueue-0" java.lang.numberformatexception: input string: "total: 8.78" @ sun.misc.floatingdecimal.readjavaformatstring(unknown source) @ java.lang.double.parsedouble(unknown source) @ pizzaorder$btnclicked.<init>(pizzaorder.java:298) @ pizzaorder$btnclicked.<init>(pizzaorder.java:295) @ pizzaorder.addlisteners(pizzaorder.java:102) @ pizzaorder.<init>(pizzaorder.java:76) @ pizzadriver$1.run(pizzadriver.java:46) @ java.awt.event.invocationevent.dispatch(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$200(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source) .
import java.awt.borderlayout; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.itemevent; import java.awt.event.itemlistener; import java.text.decimalformat; import java.text.numberformat; import javax.swing.*; /** * window ordering pizza<br> * * <hr> * date created: apr 14, 2014<br> * <hr> * @author zach latture */ public class pizzaorder extends jframe { private static final long serialversionuid = 1l; public static final double pizzacost = 8.00, taxrate = 0.0975; private jpanel headerpanel, checkpanel, buttonspanel; private jlabel pizzalbl; private jcheckbox pepperoni, sausage, peppers, onions, mushrooms, cheese; private jtextfield numberofpizzas; private jlabel totallbl, subtotallbl; private jlabel taxlbl; private numberformat fmt = new decimalformat(); private jbutton submit, cancel; private double subtotal = 0; private double tax = 0; private double total = 0; public pizzaorder() { super ("pizza order - latture"); this.setsize(250, 250); this.setdefaultcloseoperation (jframe.exit_on_close); setlayout(new borderlayout(2, 5)); initcomponents ( ); // instantiate , initial labels, textfields, etc. initheaderpanel ( ); // put label , textfield in header panel add(headerpanel, borderlayout.north); initcheckpanel ( ); // put checkboxes , totals in checkpanel add(checkpanel, borderlayout.center); initbuttonpanel ( ); // put buttons in button panel add(buttonspanel, borderlayout.south); calculate(); // calculate subtotal, tax, , total addlisteners ( ); // register listeners needed events setlocationrelativeto(null); setvisible(true); } /** * add listeners events <br> * * <hr> * date created: apr 14, 2014 <br> * date last modified: apr 14, 2014 <br> * * <hr> */ private void addlisteners ( ) { // register listeners events components // such buttons, checkboxes, etc. pepperoni.additemlistener(new listener()); sausage.additemlistener(new listener()); peppers.additemlistener(new listener()); onions.additemlistener(new listener()); mushrooms.additemlistener(new listener()); cheese.additemlistener(new listener()); submit.addactionlistener(new btnclicked()); cancel.addactionlistener(new btnclicked()); } /** * add buttons button panel <br> * * <hr> * date created: apr 14, 2014 <br> * * <hr> */ private void initbuttonpanel ( ) { buttonspanel = new jpanel(new flowlayout()); buttonspanel.add (submit); buttonspanel.add (cancel); } /** * init checkboxes , totals <br> * * <hr> * date created: apr 14, 2014 <br> * * <hr> */ private void initcheckpanel ( ) { checkpanel = new jpanel(new flowlayout(flowlayout.center, 5, 1)); checkpanel.add (pepperoni); checkpanel.add (sausage); checkpanel.add (peppers); checkpanel.add (onions); checkpanel.add (mushrooms); checkpanel.add (cheese); checkpanel.add (subtotallbl); checkpanel.add (taxlbl); checkpanel.add (totallbl); // add checkboxes , labels totals , tax } /** * add label , textfield header panel <br> * * <hr> * date created: apr 14, 2014 <br> * * <hr> */ private void initheaderpanel ( ) { headerpanel = new jpanel(new flowlayout(flowlayout.center)); headerpanel.add (pizzalbl); headerpanel.add (numberofpizzas); } /** * instantiate buttons, checkboxes, labels, etc.<br> * * <hr> * date created: apr 14, 2014 <br> * * <hr> */ private void initcomponents ( ) { submit = new jbutton("submit"); cancel = new jbutton("cancel"); fmt = numberformat.getcurrencyinstance ( ); pizzalbl = new jlabel("number of pizzas"); totallbl = new jlabel("total:"); subtotallbl = new jlabel("subtotal:"); taxlbl = new jlabel("tax: "); numberofpizzas = new jtextfield("3"); numberofpizzas.settext("1"); pepperoni = new jcheckbox("pepperoni"); sausage = new jcheckbox("sausage"); peppers = new jcheckbox("peppers"); onions = new jcheckbox("onions"); mushrooms = new jcheckbox("mushrooms"); cheese = new jcheckbox("extra cheese"); } /** * use values selected , entered determine cost<br> * * <hr> * date created: apr 14, 2014 <br> * * <hr> */ public void calculate() { subtotal = 0; double increase = 0; string input = numberofpizzas.gettext(); double val = 0, determinedtotal, determinedsubtotal, determinedtax; val = numberofpizzas.getvalue().doublevalue(); try { val = double.parsedouble(input); }catch(exception e) { system.out.print("invalid input. error: " + e.getmessage()); } determinedsubtotal = val * pizzacost; determinedtax = determinedsubtotal * taxrate; determinedtotal = determinedsubtotal + determinedtax; if(pepperoni.isselected()) { pepperoni.setselected(true); increase += val * 1.00; } if(sausage.isselected()) { sausage.setselected(true); increase += val * 1.00; } if(peppers.isselected()) { peppers.setselected(true); increase += val * .50; } if(onions.isselected()) { onions.setselected(true); increase += val * .50; } if(mushrooms.isselected()) { mushrooms.setselected(true); increase += val * .50; } if(cheese.isselected()) { cheese.setselected(true); increase += val * 1.00; } subtotallbl.settext("subotal: " +string.valueof(determinedsubtotal + increase)); taxlbl.settext("tax: " +string.valueof(determinedtax + increase)); totallbl.settext("total: " + string.valueof(determinedtotal + increase)); numberofpizzas.grabfocus ( ); numberofpizzas.selectall ( ); return; } /** * class handles itemstatechanged events checkboxes<br> * * <hr> * date created: apr 14, 2014<br> * <hr> * @author zach latture */ private class listener implements itemlistener { // appropriate method calculates results public void itemstatechanged(itemevent e) { calculate(); } } /** * class handles button clicked events both buttons<br> * * <hr> * date created: apr 14, 2014<br> * <hr> * @author zach latture */ private class btnclicked implements actionlistener { string input = totallbl.gettext(); double d = double.parsedouble(input) * 0.20; public void actionperformed(actionevent e) { object src = e.getsource(); if(src == submit) { joptionpane.showmessagedialog (null, "thank order - tip be" + fmt.format(d),"thank you.", 1); } else if(src == cancel) { joptionpane.showmessagedialog (null, "order cancelled","thank you.", 1); } } /** * reset choices defaults<br> * * <hr> * date created: apr 14, 2014 <br> * * <hr> */ private void reset ( ) { // set checkboxes , text fields original values calculate(); } } } i'm getting error mentioned above. assume it's because of parsing empty string, haven;t been able pinpoint where. it's displaying total in first line of error, yet no input has been given yet. i've been looking @ while now, , new pair experienced of eyes can see problem.
i assume it's because of parsing empty string,
no because string attempting parse total: 8.78
quick way remove label (assumming have control on label
substring("total:".length()).trim();
Comments
Post a Comment