简体   繁体   English

java.lang.numberformatexception 解析字符串输入数据库时

[英]java.lang.numberformatexception when parsing string to input into database

I'm relatively new to Java and am trying to make a POS system with a checkout.我对 Java 比较陌生,并且正在尝试制作一个带有结账功能的 POS 系统。 I want to input invoices into a database by taking the values from a checkout table.我想通过从结帐表中获取值将发票输入数据库。

I have a table (jTable2) in which each of the items in the cells are objects.我有一个表格(jTable2),其中单元格中的每个项目都是对象。 I have tried casting it to a string, and upon that failing, I used the toString() method, but I keep getting the same error.我尝试将其转换为字符串,但在失败后,我使用了 toString() 方法,但我一直收到同样的错误。 Here is my code这是我的代码

if(c<=itemcounter){
    String sql="INSERT into invoices(InvoiceID, ClientID, ProductName, ProductAmt, UnitPrice, TotalProductPrice) values (?,?,?,?,?,?)";
    Connection conn=MySqlConnection();

    PreparedStatement pstmt=conn.prepareStatement(sql);

    pstmt.setInt(1, InvoiceID);
    pstmt.setInt(2, Integer.parseInt(cIDField.getText()));
    pstmt.setString(3, (String)jTable2.getValueAt(c, 1));
    pstmt.setInt(4, Integer.parseInt(jTable2.getValueAt(c, 3).toString())); //THIS IS THE LINE I NEED HELP WITH
    pstmt.setDouble(5, Double.parseDouble((String) jTable2.getValueAt(c, 2)));
    pstmt.setDouble(6, Double.parseDouble((String) jTable2.getValueAt(c, 4)));
    pstmt.executeUpdate();
}

java.lang.numberformatexception for input string "(string that is a parsed cell c, 4 of jTable2)"输入字符串的 java.lang.numberformatexception “(作为已解析单元格 c,jTable2 的 4 个)的字符串”

I don't understand what I can do to fix it.我不明白我能做些什么来解决它。 I would appreciate any help.我将不胜感激任何帮助。 Thank you!谢谢!

Look at the line: Integer.parseInt(jTable2.getValueAt(c, 3).toString())看这行:Integer.parseInt(jTable2.getValueAt(c, 3).toString())

It get's the value from a table and converts it to an int.它从表中获取值并将其转换为 int。 Guess what happens when the value retrieved is not an int?猜猜当检索到的值不是 int 时会发生什么? That's right, you get a NumberFormatException.没错,你得到了一个 NumberFormatException。

Create a safe helper method like:创建一个安全的辅助方法,例如:

private int safeConvertToInt(String value) {
  try {
    return Integer.parseInt(value);
  }
  catch (NumberFormatException e) {
    System.out.println("Error: Could not convert |" + value + "| to int");
    return 0;
  }
}

How safeConvertToInt behaves depends upon your needs. safeConvertToInt 的行为方式取决于您的需求。 The above logs the error and returns the value zero.以上记录了错误并返回零值。 You may well want different error handling.您可能需要不同的错误处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NumberFormatException:对于输入字符串:“”解析字符串表达式 - java.lang.NumberFormatException: For input string: “” parsing string expression java.lang.NumberFormatException:对于输入字符串:使用“ 2”或“ 5”时为“” - java.lang.NumberFormatException: For input string: “” when using “2” or “5” java.lang.NumberFormatException:对于输入字符串:“1” - java.lang.NumberFormatException: For input string: " 1" 错误java.lang.NumberFormatException:对于输入字符串:“” - Error java.lang.NumberFormatException: For input string: “” java.lang.NumberFormatException:对于输入字符串:“null” - java.lang.NumberFormatException: For input string: “null” java.lang.NumberFormatException:对于输入字符串:“” - java.lang.NumberFormatException: For input string: “” java.lang.NumberFormatException:对于输入字符串:“20110328094108069414” - java.lang.NumberFormatException: For input string: “20110328094108069414” java.lang.NumberFormatException:对于输入字符串:“” - java.lang.NumberFormatException: For input string: “ ” java.lang.NumberFormatException:用于输入字符串 - java.lang.NumberFormatException: For input string java.lang.NumberFormatException:对于输入字符串:“ 8999999999999995” - java.lang.NumberFormatException: For input string: “8999999999999995”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM