java - NumberFormatException on parsing JTextField input -


the java applet has working code, think. however, not getting program executed way want to.

here message shows when runs (on parsing jtextfield input):

java.lang.numberformatexception: input string: ""     @ java.lang.numberformatexception.forinputstring(unknown source)     @ java.lang.integer.parseint(unknown source)     @ java.lang.integer.parseint(unknown source)     @ skirace.init(skirace.java:24)     @ ready.appletrunner.run(appletrunner.java:209)     @ java.lang.thread.run(unknown source) 

here code:

import java.applet.*; import java.awt.*; import javax.swing.*; import java.awt.event.*;  public class skirace extends applet implements actionlistener { int txt = 0; int txt2 = 0; int txt3 = 0; int txt4 = 0;  public void init () {     resize (300, 350);     setbackground (color.blue);     jlabel pic = new jlabel (createimageicon ("race.jpg"));      jlabel ins = new jlabel ("enter number of points beside each skier.");     ins.setfont (new font ("arial", font.bold, 12));      jlabel p1 = new jlabel (createimageicon ("player1.jpg"));     jtextfield answer = new jtextfield (3);     int txt = integer.parseint (answer.gettext ());      jlabel p2 = new jlabel (createimageicon ("player2.jpg"));     jtextfield answer2 = new jtextfield (3);     int txt2 = integer.parseint (answer2.gettext ());      jlabel p3 = new jlabel (createimageicon ("player3.jpg"));     jtextfield answer3 = new jtextfield (3);     int txt3 = integer.parseint (answer3.gettext ());      jlabel p4 = new jlabel (createimageicon ("player4.jpg"));     jtextfield answer4 = new jtextfield (3);     int txt4 = integer.parseint (answer4.gettext ());      jbutton done = new jbutton ("done");     done.setactioncommand ("done");     done.addactionlistener (this);      add (pic);     add (ins);     add (p1);     add (answer);     add (answer2);     add (answer3);     add (answer4);     add (done);  }   public void actionperformed (actionevent e) {     if (e.getactioncommand ().equals ("done"))     {         if ((txt == 7) && (txt2 == 8) && (txt3 == 6) && (txt4 == 9))         {             joptionpane.showmessagedialog (null, "you got it! great work!", "correct",                     joptionpane.information_message);         }         else         {             joptionpane.showmessagedialog (null, "you incorrect, please try again.",                     "incorrect", joptionpane.error_message);         }     } }   protected static imageicon createimageicon (string path) {     java.net.url imgurl = dice.class.getresource (path);     if (imgurl != null)     {         return new imageicon (imgurl);     }     else     {         system.err.println ("couldn't find file: " + path);         return null;     } } } 

why happening?

i think has dialog boxes or text fields input, since first program using either of 2 functions in java applet.

since line (# 24) in init() method:

int txt = integer.parseint (answer.gettext ()); 

answer have exact same text constructed (i.e. nothing - empty string). empty string not integer or parsable integer.


i recommend instead:

  • create spinnernumbermodel , keep reference model. add model in constructor of jspinner instead of jtextfield.
  • act on number (obtained querying model) either when value of spinner changes, or when user activates button, rather directly after created , before user ever sees control, let alone gets change it.

other notes:

  • don't 'shadow' variables. e.g.

    int txt4 = 0; 

    is declared class attribute, declared again local variable within init() method. e.g.

    int txt4 = integer.parseint (answer4.gettext ()); 

    to fix it, change to:

    txt4 = integer.parseint (answer4.gettext ()); // use class attribute store result 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -