Advertisement
Advertisement


Android how to convert int to String?


Question

I have an int and I want to convert it to a string. Should be simple, right? But the compiler complains it can't find the symbol when I do:

int tmpInt = 10;
String tmpStr10 = String.valueOf(tmpInt);

What is wrong with the above? And, how do I convert an int (or long) to a String?

Edit: valueOf not valueof ;)

2017/12/28
1
85
12/28/2017 6:58:00 PM

Accepted Answer

Use this String.valueOf(value);

2013/04/05
217
4/5/2013 2:43:49 PM

Normal ways would be Integer.toString(i) or String.valueOf(i).

int i = 5;
String strI = String.valueOf(i);

Or

int aInt = 1;    
String aString = Integer.toString(aInt);
2013/04/05

You called an incorrect method of String class, try:

int tmpInt = 10;
String tmpStr10 = String.valueOf(tmpInt);

You can also do:

int tmpInt = 10;
String tmpStr10 = Integer.toString(tmpInt);
2013/04/05

Use Integer.toString(tmpInt) instead.

2013/04/05