java 整数转字符串的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4105655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:46:39  来源:igfitidea点击:

Problems converting Integer to String

javaandroidstringinteger

提问by Entropy1024

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.

我正在尝试将 EditText 字段中的两个数字相加。到目前为止,我有下面的代码,我相信将 EditText 字段“pos1_deg”和“pos2_deg”转换为整数 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

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

然后 degSum 寄存器保存 deg1 和 2 的总和。到目前为止,这是正确的吗?

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:

然后输出回“结果” EditText 我需要将整数 degSum 更改为字符串。我认为正确的方法是使用下面的代码:

result.setText(degSum.toString());

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

但是,我收到“无法在原始类型 int 上调用 toString()”错误。我究竟做错了什么?

Many thanks for any help

非常感谢您的帮助

回答by Andrzej Doyle

(Assuming this is Java...)

(假设这是 Java...)

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 Integerclass statically, so instead you should use:

该消息是正确的。原始值(例如int)不能对它们调用方法,因为它们不是对象。相关的方法是Integer静态注册在类上的,因此您应该使用:

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

(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 rightnow but worth bearing in mind.)

(此方法也需要,让你要在数输出指定基可选的第二个参数,这样你就可以通过调用得到的十六进制表示Integer.toString(degSum, 16),例如,可能不是你所需要的正确的,但现在在脑海值得铭记。)

回答by CodeClimber

When you concatanate a String to a non-String the result is a String.

当您将一个字符串连接到一个非字符串时,结果是一个字符串。

e.g.

例如

int deg1 = 5;
int deg2 = 4;
result.setText("" + deg1 + deg2): // passes in "45" (a String)
result.setText("" + (deg1 + deg2)): // passes in "9" (a String)
result.setText(deg1 + deg2); // passes in 9 (an int), compile error

回答by CodeClimber

Have you tried:

你有没有尝试过:

result.setText(String.valueOf(deg_sum));

回答by Vladimir Ivanov

  1. You can try to do String.valueOf(deg_sum)

  2. You can make your degSum not int, but Integer, so the toString method will be available.

  1. 你可以尝试做 String.valueOf(deg_sum)

  2. 您可以使 degSum 不是 int,而是 Integer,因此 toString 方法将可用。