问题描述
如果我尝试
nltxt = nllen.toString();
与 nllen
是
int nllen = nl.getLength();
我得到的错误
I get the error
不能调用的toString()
上的原始类型为int。
我想整型转换为字符串,这样我就可以显示与日志条目数...为什么不工作?
I want to convert the int to string so i can display the number of entries with Log...Why doesn't it work?
推荐答案
原件没有任何字段或方法。有时,编译器将autobox你的原始入相应班级,整数
在这种情况下。也许这就是你在这种情况下,所期望的。有时,编译器将无法做到这一点。在这种情况下,它不会自动autobox它
Primitives do not have any fields or methods. Sometimes the compiler will "autobox" your primitive into the corresponding class, Integer
in this case. Perhaps that is what you expected in this case. Sometimes the compiler will not do this. In this case it does not automatically autobox it.
您有几个选择:
-
将String.valueOf(nltxt)
+ nltxt
(或者,如果你要的号码写沿着一些有用的东西,做nltxt等于+ nltxt
"" + nltxt
(or if you have something useful to write along with the number, do "nltxt equals " + nltxt
执行自动装箱手动:新的整数(nltxt)的ToString()
格式以某种自定义的方式:的String.Format(nltxt是%D这是坏的%N,nltxt)
Format it in some custom way: String.format("nltxt is %d which is bad%n", nltxt)
这篇关于Android的:不能对基本类型int调用的toString()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!