检查空的整数值文本字段错误。

huangapple go评论72阅读模式
英文:

error check the empty textField of int values

问题

以下是翻译好的代码部分:

String name = txtname.getText();
int id = Integer.parseInt(txtid.getText());
int pass = Integer.parseInt(txtpass.getText());
String day = Date.getSelectedItem().toString();

if (txtname.getText().isEmpty() || txtid.getText().isEmpty() || Date.getSelectedIndex() == 0 || event == null || time == null) {
    JOptionPane.showMessageDialog(null, "您应该添加所有信息", "错误", JOptionPane.ERROR_MESSAGE);
} else {
    ((DefaultListModel) jList1.getModel()).addElement(txtname.getText() + " " + txtpass.getText() + "(" + Date.getSelectedItem() + ") " + event + ", " + time);
    try {
        Statement stmt = con.createStatement();
        stmt.execute("INSERT INTO company(name,id) VALUES('" + name + "'," + id + ");");
        stmt.execute("INSERT INTO events(pass,time,status,day,companyId) VALUES(" + pass + ",'" + time + "','" + event + "','" + day + "'," + id + ");");

        JOptionPane.showMessageDialog(this, "记录已提交");
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, ex);
    }
}

this.txtname.setText(null);
this.txtpass.setText(null);
this.txtid.setText(null);
this.Date.setSelectedIndex(0);
this.jCheckBox1.setSelected(false);
this.jCheckBox2.setSelected(false);
this.jCheckBox3.setSelected(false);

关于错误部分:

java.lang.NumberFormatException: For input string: ""

这个错误是由于试图将空字符串解析为整数导致的。为了解决这个问题,你可以在解析之前先检查文本字段是否为空。你已经注意到在移除以下行后代码可以正常工作:

txtid.getText().isEmpty()

所以,你可以在解析之前添加一个条件来检查是否为空,如果不为空再进行解析。以下是修改后的代码:

String name = txtname.getText();
String idText = txtid.getText();
String passText = txtpass.getText();

if (idText.isEmpty()) {
    JOptionPane.showMessageDialog(null, "您应该填写ID信息", "错误", JOptionPane.ERROR_MESSAGE);
} else {
    int id = Integer.parseInt(idText);
    int pass = Integer.parseInt(passText);
    String day = Date.getSelectedItem().toString();

    if (txtname.getText().isEmpty() || Date.getSelectedIndex() == 0 || event == null || time == null) {
        JOptionPane.showMessageDialog(null, "您应该添加所有信息", "错误", JOptionPane.ERROR_MESSAGE);
    } else {
        // ... (后续代码不变)
    }

    // ... (后续代码不变)
}

请注意,我在解析之前添加了对 idText 的非空检查。这样可以避免空字符串导致的解析错误。

英文:

Here is the code:

String name = txtname.getText();
int id = Integer.parseInt(txtid.getText());
int pass = Integer.parseInt(txtpass.getText());
String day = Date.getSelectedItem().toString();
if (txtname.getText().isEmpty() ||txtid.getText().isEmpty()||Date.getSelectedIndex()==0 || event == null||time==null) {
JOptionPane.showMessageDialog(null, "you should add all information", "error", JOptionPane.ERROR_MESSAGE);
}                                       
else {
((DefaultListModel) jList1.getModel()).addElement(txtname.getText() + " " + txtpass.getText() + "(" + Date.getSelectedItem() + ") "+event+", "+time);
try{
Statement stmt = con.createStatement();
stmt.execute("INSERT INTO company(name,id) VALUES('"+name+"',"+id+");");
stmt.execute("INSERT INTO events(pass,time,status,day,companyId) VALUES("+pass+",'"+time+"','"+event+"','"+day+"',"+id+");");
JOptionPane.showMessageDialog(this, "Record Submit");
}catch(Exception ex){
JOptionPane.showMessageDialog(this, ex);
} 
}
this.txtname.setText(null);
this.txtpass.setText(null);
this.txtid.setText(null);
this.Date.setSelectedIndex(0);
this.jCheckBox1.setSelected(false);
this.jCheckBox2.setSelected(false);
this.jCheckBox3.setSelected(false);
}

This is the error:

java.lang.NumberFormatException: For input string: ""

The code is work probably with string textFields but does not work with integer textField.
It works probably if I remove:

 txtid.getText().isEmpty()

please show me how can I fix the code?

答案1

得分: 1

似乎 txtid.getText() 返回了 ""。这是否正确?
字符串 "" 不是数字,无法被 Integer.parseInt() 解析。你可以尝试使用 try 和 catch。如果它抛出 NumberFormatException 错误,你可以相应地处理它。

英文:

It seems txtid.getText() is returning "". Is that correct?
The string "" is not numeric and cannot be parsed by Integer.parseInt(). One thing you can do is use a try, catch. If it throws a NumberFormatException error, you can then handle it accordingly.

huangapple
  • 本文由 发表于 2020年4月10日 21:27:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61141293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定