Convert Int to String

I'm trying to make a button to register to the bank but I'm having trouble converting the int to String.

Follows my line of Code:

 private void jBtnCadastroActionPerformed(java.awt.event.ActionEvent evt) {                                             
       f = new Funcionario(String.valueOf(jtxtFuncionario.getText()),
                jTxtDepartamento.getText());

       jtxtFuncionario.setText("");
        jTxtDepartamento.setText("");

    f.Save();
    }    

The following error appears in the line " f = new Funcionario(String.valueOf(jtxtFuncionario.getText())":

String cannot be converted to string

Author: Edmundo, 2018-05-30

4 answers

The Method getText(), as the documentation itself says, it returns a type String, so there is no need to cast for string.

f = new Funcionario(jtxtFuncionario.getText());

If you are passing integer values to a text field, pro java this makes no difference as the method will treat all content as a String. If your class expects to get an integer, then the correct cast would be for int and not for string:

f = new Funcionario(Integer.valueOf(jtxtFuncionario.getText()));
 1
Author: , 2018-06-04 12:59:03

Good according to this code snippet:

F = new official (String.valueOf (jtxtfunctionary.getText()), jtxtdepartment.getText ());

You are trying to take the string part of a text field, which is wrong, because even if you put numbers in the text field you will 'take' a string type, so that " String.ValueOf " is disposable.

Now if you are trying to convert String to Integer you can do the next:

String text = jtxtfunctionary.getText(); int texpoint = Integer.ParseInt (text);

 0
Author: Aprendendo, 2018-11-13 19:20:46

It is not necessary to perform the conversion of the object to String, since the return of:

jtxtFuncionario.getText()

Is already a String!

If you need to convert this string object to an Integer, you can write the following line of code:

Integer.valueOf(suaVariavelString);

This line will perform the conversion of a string to integer. it is worth noting that non-integer characters may generate errors. Take due care to perform the check of the data entry on your screen (mask)

Note: pay attention to null objects, they usually cause errors.

 0
Author: Alysson Chicó, 2018-11-13 19:33:11
    int i =10;
    String inteiro;

    inteiro = i + "";

This is the simplest way to do this

 0
Author: Marcio Caldas, 2018-11-13 20:00:24