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
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()));
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);
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.
int i =10;
String inteiro;
inteiro = i + "";
This is the simplest way to do this