I have this piece of code:
ed = (EditText) findViewById (R.id.box); int x = 10; ed.setText (x);
It turns out to be an error. I know I have to change it to string, but how do I do this?
I've tried x.toString()
, but it can't be compiled.
Conversion of an integer into a string by using to_string() method. The to_string() method accepts a single integer and converts the integer value or other data type value into a string.
The easiest way to convert int to String is very simple. Just add to int or Integer an empty string "" and you'll get your int as a String. It happens because adding int and String gives you a new String. That means if you have int x = 5 , just define x + "" and you'll get your new String.
The Best Answer is you have to used. String value= et. getText(). toString(); int finalValue=Integer.
Use +
, the string concatenation operator:
ed = (EditText) findViewById (R.id.box); int x = 10; ed.setText(""+x);
or use String.valueOf(int)
:
ed.setText(String.valueOf(x));
or use Integer.toString(int)
:
ed.setText(Integer.toString(x));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With