Computer Science Canada

Converting an Integer to a String?

Author:  Delta [ Fri Jul 09, 2004 2:42 pm ]
Post subject:  Converting an Integer to a String?

Can anyone tell me how to convert an Integer to a String?
I've tried this

String converted;
int num;

converted = (int)num;... but I got this error > Incompatible Types
converted = (String)num; ... but I got his error > Inconvertible Types

and ya... I'm not sure what else to try... I've looked on http://java.sun.com but all I found were library breakdowns... and those don't help worth shit.

Author:  Tony [ Fri Jul 09, 2004 3:00 pm ]
Post subject: 

eh? Confused
code:

Integer num = new Integer(5);
String word;

word = num.toString;

Author:  wtd [ Fri Jul 09, 2004 6:28 pm ]
Post subject: 

code:
C:\> groovysh
Lets gets Groovy!
Hit carriage return twice to execute a command
The command 'quit' will terminate the shell
groovy> String
groovy>
class java.lang.String
groovy> String.valueOf 67
groovy>
'67'
groovy> System.exit 0


Smile

Alternately...

code:
int i = 42;
String s = Integer.toString(i);

Author:  bugzpodder [ Fri Jul 09, 2004 7:50 pm ]
Post subject: 

something like
code:

int i=42;
String s=i+"";

probably also works

Author:  wtd [ Fri Jul 09, 2004 7:57 pm ]
Post subject: 

bugzpodder wrote:
something like
code:
int i=42;
String s=i+"";

probably also works


Make that

code:
int i=42;
String s="" + i;


And you're right, but that approach is also inefficient.

Author:  Delta [ Sat Jul 24, 2004 8:20 am ]
Post subject: 

Very inefficient indeed... tisk tisk... lol
anyways ya Tony your way works best compared to the other peoples which weren't really converting but anyways... ya :S... have a nice day

Author:  rizzix [ Sun Jul 25, 2004 1:17 am ]
Post subject: 

actually that approach isin't "very" inefficient. it does the same thing:
creates a Interger object and calls the toString() method.

the only possibly additional thing it does is a concatenation with a "" empty string. now i'm pretty sure the javac optimizes this to simply a .toString() call.


: