Posted: 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.
Sponsor Sponsor
Tony
Posted: Fri Jul 09, 2004 3:00 pm Post subject: (No subject)
Posted: Fri Jul 09, 2004 6:28 pm Post subject: (No 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
Alternately...
code:
int i = 42;
String s = Integer.toString(i);
bugzpodder
Posted: Fri Jul 09, 2004 7:50 pm Post subject: (No subject)
something like
code:
int i=42;
String s=i+"";
probably also works
wtd
Posted: Fri Jul 09, 2004 7:57 pm Post subject: (No 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.
Delta
Posted: Sat Jul 24, 2004 8:20 am Post subject: (No 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
rizzix
Posted: Sun Jul 25, 2004 1:17 am Post subject: (No 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.