Convert Int to String

Asked

Viewed 788 times

2

I’m trying to make a button to register to the bank but am having difficulties in 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();
    }    

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

string cannot be converted to string

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, java does not make a difference, as 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()));
  • Forgive my friend first, the message is "string cannot be converted to Integer"

  • 2

    @Edmundo error does not match the code. Edit the question and provide a [mcve] because by the code presented, the answer solves the problem.

0

Good according to this excerpt from the code:

f = new Function(String.valueOf(jtxtFunctioning.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 'catch' a String type, then this "String.Valueof" is disposable.

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

String text = jtxtFunctioning.gettext(); int textoInt = Integer.Parseint(text);

0

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 into an integer. It is valid to emphasize that non-integer characters can generate errors. Please take care to check the data entry on your screen (Mask)

NOTE: Be aware of null objects, usually cause errors.

0

    int i =10;
    String inteiro;

    inteiro = i + "";

That’s the simplest way to do it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.