swing - Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String -
i've been having problems running program compiles doesn't run properly. when run , attempt perform calculations spits out bunch errors. think has variable types. here program:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class area extends jframe implements actionlistener, itemlistener{ //row 1 jpanel row1 = new jpanel(); jlabel select = new jlabel("please select caculate area , volume of."); //row 2 jpanel row2 = new jpanel(); jcheckbox circle = new jcheckbox("circle", false); jcheckbox cube = new jcheckbox("cube", false); //row 3 jpanel row3 = new jpanel(); jlabel radlab = new jlabel("radius of circle (in cm)"); jtextfield rad = new jtextfield(3); jlabel sidelab = new jlabel("a side of cube (in cm)"); jtextfield side = new jtextfield(3); //row4 jpanel row4 = new jpanel(); jbutton solve = new jbutton("solve!"); //row 5 jpanel row5 = new jpanel(); jlabel areacallab = new jlabel("area"); jtextfield areacal = new jtextfield(10); jlabel volumelab = new jlabel("volume"); jtextfield volume = new jtextfield(10); public area(){ settitle("area caculator"); setsize(500,400); setdefaultcloseoperation(jframe.exit_on_close); setvisible(true); //disables text areas rad.setenabled(false); side.setenabled(false); areacal.setenabled(false); volume.setenabled(false); //add listeners circle.additemlistener(this); cube.additemlistener(this); solve.addactionlistener(this); flowlayout 1 = new flowlayout(flowlayout.center); setlayout(one); row1.add(select); add(row1); row2.add(circle); row2.add(cube); add(row2); row3.add(radlab); row3.add(rad); row3.add(sidelab); row3.add(side); add(row3); row4.add(solve); add(row4); row5.add(areacallab); row5.add(areacal); row5.add(volumelab); row5.add(volume); add(row5); } public void circlepick(){ //cube.setcurrent(false); cube.setenabled(false); rad.setenabled(true); } public void cubepick(){ circle.setenabled(false); side.setenabled(true); } @override public void itemstatechanged(itemevent event) { object item = event.getitem(); if (item == circle){ circlepick(); } else if (item == cube){ cubepick(); } } @override public void actionperformed(actionevent evt){ //string radi = rad.gettext(); //string sid = side.gettext(); //circlesolve(); //cubesolve(); string radi = rad.gettext(); string sid = side.gettext(); double radius = double.parsedouble(radi); double length = double.parsedouble(sid); double cirarea = math.pi * math.pow(radius, 2); double cirvolume = (4.0 / 3) * math.pi * math.pow(radius, 3); double cubearea = math.pow(length, 2); double cubevolume = math.pow(length, 3); areacal.settext("" + cirarea + cubearea + ""); volume.settext("" + cirvolume + cubevolume + ""); } public static void main(string[] args) { area = new area(); } }
here errors printing out when attempting perform math (sorry long).
exception in thread "awt-eventqueue-0" java.lang.numberformatexception: empty string
@ sun.misc.floatingdecimal.readjavaformatstring(floatingdecimal.java:1038)
@ java.lang.double.parsedouble(double.java:548)
@ area.actionperformed(area.java:112)
...
thanks in advance help!
when calling functions:
double radius = double.parsedouble(radi); double length = double.parsedouble(sid);
either radi
or sid
empty string
, thats what
java.lang.numberformatexception: empty string
tells you.
might consider adding system.out.println(raid + ", " + sid)
before parsing check values empty strings , make sure, strings not empty.
double.parsedouble(string s)
throws numberformatexception
when given string s
can not parsed double
value.
Comments
Post a Comment