我试图做一个货币兑换程序,但是如果第二个输入(这里是s1)是空的,程序就会给出NumberFormatException:Empty String error。当第一个(在这里)是空的时候,它就能工作了。因此,我想知道是否有一个选择的方式,使它有选择性的一个按钮。为什么当第二个字段为空时,它就不能工作?
public class kanvas1 implements ActionListener,WindowListener{
private JTextField tf;
private JTextField tf1;
private JTextField tf2;
private JTextField tf3;
private JLabel lb;
private JLabel lb1;
private JButton bt;
private JButton bt1;
public kanvas1()
{
tf=new JTextField();
tf1=new JTextField();
tf2=new JTextField();
tf3=new JTextField();
lb=new JLabel("$");
lb1=new JLabel("TL");
bt=new JButton("Cevir");
bt1=new JButton("Sıfırla");
pencere();
}
public void pencere() {
tf.setBounds(50,20,150,50);
tf1.setBounds(50,80, 150, 50);
tf2.setBounds(220,20,150,50);
tf3.setBounds(220,80,150,50);
lb.setBounds(30,20,20, 50);
lb1.setBounds(30,80,20,50);
bt.setBounds(400,20,100, 50);
bt1.setBounds(400,80,100,50);
bt.addActionListener(this);
bt1.addActionListener(this);
JFrame ab=new JFrame();
ab.setVisible(true);
ab.setSize(600,200);
ab.setLayout(null);
ab.add(tf);ab.add(tf1);ab.add(tf2);ab.add(tf3);ab.add(bt);ab.add(bt1);ab.add(lb);ab.add(lb1);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s=tf.getText(); //problem is here
double a=Double.parseDouble(s);
double c=a*5.44;
String result=String.valueOf(c);
tf2.setText(result);
}
});
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s1=tf1.getText();
//and here
double b=Double.parseDouble(s1);
double d=b*0.18;
String result1=String.valueOf(d);
tf3.setText(result1);
}
});
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
tf.setText("");
tf1.setText("");
tf2.setText("");
tf3.setText("");
}
});
}
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String args[]) {
new kanvas1();
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}发布于 2019-03-16 18:58:49
您可以在像这样的解析之前添加一个检查:
if (s1.isEmpty())
return;它失败了,因为Double.parseDouble(String arg)试图将作为参数传递的字符串转换为字符串表示的双值。这意味着字符串如"12“、"12.0”等。是有效的输入,而"12a“、"ac”、“”等字符串则是有效输入。肯定会失败;因为,毕竟,12a不是一个双(它甚至不是一个数字!)。
如果文本字段是空的,并且执行getText(),那么您将检索的是"" (空字符串),而不是null。
因此,如果您想作出任何有条件的决定,您的条件基于检索到的文本为empty i.e. ""而不是null。
发布于 2019-03-16 19:21:40
不必为相同的按钮编写两次类似的addActionListener,您可以在单个addActionListener中编写它。此外,避免将任何字段保持为空,您将得到NumberFormatException ,因为您的 JTextField 给出的是空白值或其他不是双值的值。使用Double.parseDouble进行解析时(“12.34”)应该始终包含字符串格式的双值。使用rest其他值进行解析会引发NumberFormatException
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s=tf.getText(); //problem is here
double a=Double.parseDouble(s);
double c=a*5.44;
String result=String.valueOf(c);
tf2.setText(result);
}
});
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s1=tf1.getText();
//and here
double b=Double.parseDouble(s1);
double d=b*0.18;
String result1=String.valueOf(d);
tf3.setText(result1);
}
});将上述代码替换为:
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s=tf.getText();
double a=Double.parseDouble(s);
double c=a*5.44;
String result=String.valueOf(c);
tf2.setText(result);
String s1=tf1.getText();
double b=Double.parseDouble(s1);
double d=b*0.18;
String result1=String.valueOf(d);
tf3.setText(result1);
}
});https://stackoverflow.com/questions/55200416
复制相似问题