29

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.

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
Jason
  • 318
  • 1
  • 4
  • 6
  • 1
    In this case, ask a new question re: `char`. (Hint: the answer is to turn the `char` into a `String`, and `Integer.parseInt()` the string.) – Matt Ball Jul 12 '11 at 04:25
  • why you not try it from google it s so easy to get this answer? – Nikunj Patel Jul 12 '11 at 04:42
  • Please don't ask more than one question at a time in a post. I have edited out your second question, ask this as a new question instead. – Kev Jul 13 '11 at 01:23
  • Possible duplicate of [How to convert from int to String?](http://stackoverflow.com/questions/4105331/how-to-convert-from-int-to-string) – max630 Mar 30 '16 at 21:11

5 Answers5

88

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));
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
9

try Integer.toString(integer value); method as

ed = (EditText)findViewById(R.id.box);
int x = 10;
ed.setText(Integer.toString(x));
Deepak Swami
  • 3,838
  • 1
  • 31
  • 46
Rob
  • 4,733
  • 3
  • 32
  • 29
2

Try using String.format() :

ed = (EditText) findViewById (R.id.box); 
int x = 10; 
ed.setText(String.format("%s",x)); 
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Ti Kanon
  • 27
  • 3
1

ed.setText (String.ValueOf(x));

Malik Ali
  • 175
  • 5
0

Use this in your code:

String.valueOf(x);
Aparajita Sinha
  • 514
  • 1
  • 10
  • 21