100

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 ;)

Ignacio Ara
  • 2,476
  • 2
  • 26
  • 37
JB_User
  • 3,117
  • 7
  • 31
  • 51

4 Answers4

256

Use this String.valueOf(value);

URAndroid
  • 6,177
  • 6
  • 30
  • 40
33

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);
K_Anas
  • 31,226
  • 9
  • 68
  • 81
16

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);
BamsBamx
  • 4,139
  • 4
  • 38
  • 63
8

Use Integer.toString(tmpInt) instead.

Karakuri
  • 38,365
  • 12
  • 84
  • 104