问题位于:java.lang.Integer.parseInt(未知源)

时间:2019-06-05 22:31:27

标签: java numberformatexception

  

我想提款(tfSommeRetrait),我在   ligne:           int res = Integer.parseInt(som1)-Integer.parseInt(som2.replace(“”,“”));

            public void retraitNormal() throws SQLException {

        try {
            ConnectionMysql connectionMysql = new ConnectionMysql();//instance of connection
            Connection connection = connectionMysql.getConnection();

            Statement statement = connection.createStatement();
            String  som1 = "SELECT SOMME FROM compte WHERE NUM_Compte ='"+tfNumCRetrait.getText()+"'";
            String som2 = tfSommeRetrait.getText();

            int res = Integer.parseInt(som1) - Integer.parseInt(som2.replace(" ",""));

            String resultat = String.valueOf(res);

            String query = "UPDATE compte SET SOMME = 'resultat' WHERE NUM_Compte='"+tfSommeRetrait.getText()+"' ";
            statement.executeUpdate(query);

        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
  

java.lang.NumberFormatException:对于输入字符串:“ SELECT SOMME FROM   compte WHERE NUM_Compte = '12'“             在java.lang.NumberFormatException.forInputString(未知来源)             在java.lang.Integer.parseInt(未知来源)             在java.lang.Integer.parseInt(未知来源)             在Caissier.Caissier.retraitNormal(Caissier.java:250)

1 个答案:

答案 0 :(得分:1)

在您的代码中,当它执行 Integer.parseInt(som1)时,变量som1的值=“ SELECT SOMME FROM compte WHERE NUM_Compte ....”,因此这是正常的错误。如果想将其som1字符串解析为整数,则必须为数字。

String  som1 = "SELECT SOMME FROM compte WHERE NUM_Compte ='"+tfNumCRetrait.getText()+"'";
String som2 = tfSommeRetrait.getText();

int res = Integer.parseInt(som1) - Integer.parseInt(som2.replace(" ",""));