user1341676
user1341676

Reputation:

Cannot convert int to String

This doesn't work:

int number = 1;
String numberstring = IntToString(number);

I get "The method IntToString(int) is undefined for the type (myclassname)"

I must be tired here. What the heck am I forgetting?

Upvotes: 2

Views: 7883

Answers (8)

user1341676
user1341676

Reputation:

These ones are the ones that seems to work "out of the box", without importing any special classes:

String numberstring = String.valueOf(number);
String numberstring = Integer.toString(number);
String numberstring = number + "";

Upvotes: 2

Buhake Sindi
Buhake Sindi

Reputation: 89189

In Java, int to String is simple as

String numberstring = String.valueOf(number);

This applies to Android too.

Upvotes: 1

jeet.chanchawat
jeet.chanchawat

Reputation: 2595

int number=1;
String S = String.valueOf(number);

try this code... works for me :)

Upvotes: 1

Chad Campbell
Chad Campbell

Reputation: 321

You can do either:

 String numberstring = number.toString();

or

String numberstring = Integer.toString(number)

or (as a tricky thing sometimes I do this)

String numberstring = 1 + "";

Upvotes: 3

Praveenkumar
Praveenkumar

Reputation: 24506

Are you expecting this -

int numb = 1;
String val = String.valueOf(numb);

Upvotes: 1

gtiwari333
gtiwari333

Reputation: 25164

Why not this :

int number = 1;
String numberstring = number+"";

Also make sure that :

Is there IntToString method in your class file - myclassname? Is the arguement types are matching?

Upvotes: 2

user1340450
user1340450

Reputation:

One way to do this with very little code would be like this:

int number = 1;
String numberstring = number + "";

Upvotes: 1

pacman
pacman

Reputation: 1061

Try this.

int number = 1;
String numberstring = Integer.toString(number);

Upvotes: 4

Related Questions