简体   繁体   English

无法将Int转换为String

[英]cannot convert an Int to String

I want to convert a int to String in java but I can't : 我想将int转换为java中的String,但是我不能:

this is the code I used : 这是我使用的代码:

 jTextField1.setText((String)l.getCode());

and this is the error I got : 这是我得到的错误:

Inconvertible types
required:java.lang.String
found:   int

您不能将int类型转换为string尝试下面的代码。

jTextField1.setText(String.valueOf(l.getCode()));

Try something like this: 尝试这样的事情:

Integer.valueOf(l.getCode()).toString()

You cannot convert int simple type to String object. 您不能将int简单类型转换为String对象。

也许您可以尝试:

String.valueOf(int)

这应该是您要寻找的:

jTextField1.setText(String.valueOf(l.getCode()))
jTextField1.setText(String.valueOf(l.getCode()));

There are three ways you convert an integer to string firstly using the built-in converter, 首先使用内置转换器将整数转换为字符串的三种方法,

jTextField1.setText("" + l.getCode())

Secondly you could use the static method toString(int) of the Integer class, 其次,您可以使用Integer类的静态方法toString(int),

jTextField1.setText(Integer.toString(l.getCode()))

You could also use a formatter but this in not recommended as it just makes the code cumbersome and difficult to understand. 您也可以使用格式化程序,但不建议使用此格式化程序,因为它只会使代码繁琐且难以理解。

jTextField1.setText(String.format("%d", l.getCode()))

尝试这个:

jTextField1.setText(Integer.toString(l.getCode()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM