英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论