解析 JTextField 输入时出现 NumberFormatException
NumberFormatException on parsing JTextField input
Java 小程序有一个工作代码,我想。但是,我没有按照我希望的方式执行我的程序。
这是它运行时显示的消息(在解析 JTextField
输入时):
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at skiRace.init(skiRace.java:24)
at ready.AppletRunner.run(AppletRunner.java:209)
at java.lang.Thread.run(Unknown Source)
这是我的代码:
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 the 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 are 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;
}
}
}
为什么会这样?
我认为它与带有输入的对话框或文本字段有关,因为这是我的第一个程序,在 Java 小程序中使用这两个功能之一。
因为这一行 (#24) 在 init()
方法中:
int txt = Integer.parseInt (answer.getText ());
answer
将具有与构造时完全相同的文本(即什么都没有 - 一个空字符串)。空字符串不是整数或不可解析为整数。
我会推荐:
- 创建一个
SpinnerNumberModel
并保留对该模型的引用。在 JSpinner
的构造函数中添加模型而不是 JTextField
。
- 在微调器的值发生变化时或在用户激活按钮时对数字(通过查询模型获得)进行操作,而不是在创建按钮之后和用户看到控件之前直接进行操作,让一个人去改变它。
其他说明:
不要'shadow'变量。例如
int txt4 = 0;
被声明为 class 属性,但在 [=14] 中又被 声明为 的 局部变量 =] 方法。例如
int txt4 = Integer.parseInt (answer4.getText ());
要修复它,请将其更改为:
txt4 = Integer.parseInt (answer4.getText ()); // use class attribute to store result
Java 小程序有一个工作代码,我想。但是,我没有按照我希望的方式执行我的程序。
这是它运行时显示的消息(在解析 JTextField
输入时):
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at skiRace.init(skiRace.java:24)
at ready.AppletRunner.run(AppletRunner.java:209)
at java.lang.Thread.run(Unknown Source)
这是我的代码:
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 the 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 are 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;
}
}
}
为什么会这样?
我认为它与带有输入的对话框或文本字段有关,因为这是我的第一个程序,在 Java 小程序中使用这两个功能之一。
因为这一行 (#24) 在 init()
方法中:
int txt = Integer.parseInt (answer.getText ());
answer
将具有与构造时完全相同的文本(即什么都没有 - 一个空字符串)。空字符串不是整数或不可解析为整数。
我会推荐:
- 创建一个
SpinnerNumberModel
并保留对该模型的引用。在JSpinner
的构造函数中添加模型而不是JTextField
。 - 在微调器的值发生变化时或在用户激活按钮时对数字(通过查询模型获得)进行操作,而不是在创建按钮之后和用户看到控件之前直接进行操作,让一个人去改变它。
其他说明:
不要'shadow'变量。例如
int txt4 = 0;
被声明为 class 属性,但在 [=14] 中又被 声明为 的 局部变量 =] 方法。例如
int txt4 = Integer.parseInt (answer4.getText ());
要修复它,请将其更改为:
txt4 = Integer.parseInt (answer4.getText ()); // use class attribute to store result