Java – the problem of converting integer to string
I want to add two numbers from the EditText field So far, I have the following code that I believe converts the EditText field 'pos1_ deg’& ‘pos2_ DEG 'becomes an integer deg1 & deg2
deg1 = Integer.parseInt(pos1_deg.getText().toString()); deg2 = Integer.parseInt(pos2_deg.getText().toString());
Then I can add them together by doing the following
degSum = deg1 + deg2
The degsum register then holds the sum of deg1 & Is this correct so far?
Then output back to 'result' EditText. I need to change the integer degsum to a string I think the correct way is to use the following code:
result.setText(degSum.toString());
But I got an error that 'tostring() cannot be called on primitive type int' What on earth did I do wrong?
Thank you very much for any help
Solution
(suppose this is Java...)
The news is correct Primitive values, such as int, cannot call methods on them because they are not objects The related methods are statically registered on the integer class, so you should use:
result.setText(Integer.toString(degSum));
(this method also takes an optional second parameter that allows you to specify the cardinality of the number to be output, so you can get the hexadecimal representation by calling integer. ToString (degsum, 16) It may not be what you need now, but it's worth remembering.)