Convert Int to String

2

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

Follow 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();
    }    

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

  

string can not be converted to string

    
asked by anonymous 30.05.2018 / 20:32

4 answers

1

The method getText() , as the documentation itself says, returns a type String , so it is not necessary to cast to string.

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

If you are passing integer values to a text field, pro java does not make a difference because the method will treat all content as 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()));
    
30.05.2018 / 20:33
0

Good according to this snippet of code:

f = new Official (String.valueOf (jtxtFuncionario.getText ()),                 jTxtDepartment.getText ());

You are trying to get the String part of a text field, which is wrong, because even if you put numbers in the text field you will 'get' a String type, then this 'String.ValueOf' is disposable .

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

String text = jtxtFuncionario.getText (); int textInt = Integer.ParseInt (text);

    
13.11.2018 / 20:20
0

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

jtxtFuncionario.getText()

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 convert a string to integer. it is worth noting that non-integer characters may generate errors. Take care to perform data entry verification on your screen (Mask)

NOTE: Beware of null objects, often leading to errors.

    
13.11.2018 / 20:33
0
    int i =10;
    String inteiro;

    inteiro = i + "";

This is the easiest way to do this

    
13.11.2018 / 21:00