103

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

4
  • 7
    valueOf big OOOOO Commented Apr 5, 2013 at 14:41
  • 2
    should be String.valueOf(tmpInt) (note the "O") Commented Apr 5, 2013 at 14:42
  • 1
    Yes, I see. Typo on my part. Thanks. Commented Apr 14, 2013 at 20:12
  • 1
    What happens if you do this String tmpStr10 = tmpInt+""; Simply concatenating an empty string at the end. Commented Apr 20, 2015 at 8:38

4 Answers 4

260

Use this String.valueOf(value);

Sign up to request clarification or add additional context in comments.

Comments

34

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

Comments

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

Comments

8

Use Integer.toString(tmpInt) instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.