Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Converting an Integer to a String?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Delta




PostPosted: 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
Sponsor
sponsor
Tony




PostPosted: Fri Jul 09, 2004 3:00 pm   Post subject: (No subject)

eh? Confused
code:

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

word = num.toString;
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




PostPosted: 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


Smile

Alternately...

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




PostPosted: Fri Jul 09, 2004 7:50 pm   Post subject: (No subject)

something like
code:

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

probably also works
wtd




PostPosted: 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




PostPosted: 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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: