0

In my private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) I have error message:

incompatible types: String cannot be converted to int

My first field (IdentCat) is normaly an Int. However I don't know how to do this in INT ?

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

More of code

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();
    }         
Logan
  • 926
  • 1
  • 10
  • 18
juliee
  • 45
  • 1
  • 7
  • You can’t pass a string value where an int is required. You need to call the ParseInt function – King May 28 '18 at 18:56
  • 1
    Possible duplicate of [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – Logan May 28 '18 at 18:57

2 Answers2

5

Use Integer.parseInt("123456").

Logan
  • 926
  • 1
  • 10
  • 18
AndrejH
  • 2,028
  • 1
  • 11
  • 23
  • like that ??? ap.setIdentCat (Integer.parseInt (jTextIdent.getText ())); – juliee May 28 '18 at 19:01
  • 1
    You should do some error handling too like what if 23k comes in. The String so according to me you should first check string.isDigit then do parseInt – Raman Mishra May 28 '18 at 19:18
3

Use Integer.valueOf("string") as alternative too! This will return an Integer object.

String number = "10";
Integer result = Integer.valueOf(number);
Keshan Nageswaran
  • 8,060
  • 3
  • 28
  • 45