问题转换整数​​转字符串整数、字符串、问题

由网友(提刀斩乾坤)分享简介:我想从EditText上田两个数字加在一起。到目前为止,我有以下的code我相信转换的EditText场pos1_deg'和; pos2_deg成整数deg1和放大器; deg2。I am trying to add two numbers together from EditText fields. So far...

我想从EditText上田两个数字加在一起。到目前为止,我有以下的code我相信转换的EditText场pos1_deg'和; pos2_deg成整数deg1和放大器; deg2。

I am trying to add two numbers together from EditText fields. So far I have the code below that I believe converts the EditText field 'pos1_deg' & 'pos2_deg' into integers deg1 & deg2.

deg1 = Integer.parseInt(pos1_deg.getText().toString());
deg2 = Integer.parseInt(pos2_deg.getText().toString());

我可以然后执行以下操作来添加在一起

I could then do the following to add them together

degSum = deg1 + deg2

和再degSum寄存器保存deg1与放大器的总和; 2.这是正确的那么远?

And then the degSum register holds the sum of deg1 & 2. Is this correct so far?

然后再输出回结果的EditText我需要改变整degSum成一个字符串。我认为正确的方法是使用下面的code:

Then to output back to the 'result' EditText I need to change the integer degSum into a string. I thought the correct way was to use the code below:

result.setText(degSum.toString());

不过,我得到一个无法调用的toString()的基本类型int的错误。我究竟做错了什么?

However I get an 'Cannot invoke toString() on the primitive type int' error. What am I doing wrong?

非常感谢您的帮助

推荐答案

(假设这就是Java ...)

(Assuming this is Java...)

该消息是正确的。原始值(如 INT )不能对他们调用的方法,因为它们不是对象。相关的方法,而不是在整数类注册静态,所以不是你应该使用:

The message is correct. Primitive values (such as int) cannot have methods invoked on them as they are not objects. The associated methods are instead registered on the Integer class statically, so instead you should use:

result.setText(Integer.toString(degSum));

(这个方法也有一个可选的第二个参数,可以让你指定要在数字输出的基础上,这样你就可以通过调用获得十六进制再presentation Integer.toString(degSum 16)例如,可能不是你所需要的右键的,但现在值得铭记。)

(This method also takes an optional second argument that lets you specify the base that you want the number output in, so you can get the hexadecimal representation by calling Integer.toString(degSum, 16) for example. Probably not what you need right now but worth bearing in mind.)

阅读全文

相关推荐

最新文章