
-----------------------------------
Delta
Fri Jul 09, 2004 2:42 pm

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.

-----------------------------------
Tony
Fri Jul 09, 2004 3:00 pm


-----------------------------------
eh? :? 

Integer num = new Integer(5);
String word;

word = num.toString;


-----------------------------------
wtd
Fri Jul 09, 2004 6:28 pm


-----------------------------------
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

:-)

Alternately...

int i = 42;
String s = Integer.toString(i);

-----------------------------------
bugzpodder
Fri Jul 09, 2004 7:50 pm


-----------------------------------
something like

int i=42;
String s=i+"";

probably also works

-----------------------------------
wtd
Fri Jul 09, 2004 7:57 pm


-----------------------------------
something like
int i=42;
String s=i+"";

probably also works

Make that 

int i=42;
String s="" + i;

And you're right, but that approach is also inefficient.

-----------------------------------
Delta
Sat Jul 24, 2004 8:20 am


-----------------------------------
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

-----------------------------------
rizzix
Sun Jul 25, 2004 1:17 am


-----------------------------------
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.
