【问题标题】:Convert String in Int在 Int 中转换字符串
【发布时间】:2018-11-07 09:19:28
【问题描述】:

在我的私人 void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) 我有错误信息:

不兼容的类型:String 无法转换为 int

我的第一个字段(IdentCat)通常是一个 Int。 但是我不知道如何在 INT 中做到这一点?

ap.setIdentCat(jTextIdent.getText().toUpperCase());

更多代码

private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        Categorie ap = new Categorie();
        ap.setIdentCat(jTextIdent.getText().toUpperCase());
        ap.setDenomCat(jTextDescr.getText());

        boolean ok = daoCat.insertCategorie(ap);
        if (!ok)
            JOptionPane.showMessageDialog(null,"Insertion impossible !","Avertissement",JOptionPane.ERROR_MESSAGE);
        this.dispose();
    }         

【问题讨论】:

标签: java string


【解决方案1】:

使用Integer.parseInt("123456")

【讨论】:

  • 这样??? ap.setIdentCat(Integer.parseInt(jTextIdent.getText()));
  • 你也应该做一些错误处理,比如如果 23k 进来了。字符串所以根据我你应该先检查 string.isDigit 然后做 parseInt
【解决方案2】:

也可以使用Integer.valueOf("string") 替代!这将返回一个 Integer 对象。

String number = "10";
Integer result = Integer.valueOf(number);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-09
    • 2010-12-15
    • 2011-04-26
    • 2011-07-11
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多