button - Errors with java applet -
i trying write applet calculate average amount of 4 input boxes or clear fields depending on box clicked. think have right, there error somewhere causing following error statement appear: exception in thread "awt-eventqueue-0" java.lang.numberformatexception: input string: " 10" here's have far:
import java.applet.*; import java.awt.*; import java.awt.event.*; public class blooddriveaverage extends applet implements actionlistener { public void init() { label title = new label("blood drive!"); setbackground(color.red); label label1 = new label("department 1 amount: "); textfield1 = new textfield(" "); avg = new button("average"); clear = new button ("clear fields"); avg.addactionlistener(this); clear.addactionlistener(this); label label2 = new label("department 2 amount: "); textfield2 = new textfield(" "); label label3 = new label("department 3 amount: "); textfield3 = new textfield(" "); label label4 = new label("department 4 amount: "); textfield4 = new textfield(" "); add(title); add(label1); add(textfield1); add(label2); add(textfield2); add(label3); add(textfield3); add(label4); add(textfield4); add(avg); add(clear); avg.setbackground(color.white); clear.setbackground(color.orange); } public void actionperformed(actionevent e) { setlayout(new flowlayout()); double average = 0; int[] myarray = new int[3]; myarray[0] = integer.parseint(textfield1.gettext()); myarray[1] = integer.parseint(textfield2.gettext()); myarray[2] = integer.parseint(textfield3.gettext()); myarray[3] = integer.parseint(textfield4.gettext()); if (e.getsource() == avg) { for(int = 0; < myarray.length; i++) { average += myarray[i]; } average /=4; label avgfield = new label("average is" + average); } else { textfield1.settext(" "); textfield2.settext(" "); textfield3.settext(" "); textfield4.settext(" "); } } textfield textfield1, textfield2, textfield3, textfield4; button avg; button clear; }
read exception. thrown @ line:
myarray[0] = integer.parseint(textfield1.gettext());
the problem can't parse string number because has spaces (e.g. 10
). need removed leading , trailing whitespace trim():
myarray[0] = integer.parseint(textfield1.gettext().trim());
that should fix numberformatexception, other errors remain.
Comments
Post a Comment