Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 19th, 2005, 04:58 PM
Ace23 Ace23 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 Ace23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 8 m 30 sec
Reputation Power: 0
String to int???

can somebody plz give me an example of how to turn a string into an int....i look in the java api and couldn't find it

Reply With Quote
  #2  
Old September 20th, 2005, 09:28 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Code:
String str = "123";
int i = Integer.parseInt(str);

Reply With Quote
  #3  
Old September 20th, 2005, 10:59 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
or if you want to do it the easy way:

int i;
String str = "123";
i = str - 0;


Reply With Quote
  #4  
Old September 20th, 2005, 02:10 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Oh sure, but what fun is the easy way?

Reply With Quote
  #5  
Old November 30th, 2005, 04:13 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 734 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 11 h 57 m 2 sec
Reputation Power: 4
@michaelsoft: That is really not possible in Java (nor in C actually). The minus operator is not overloaded to handle strings. In the case of the plus, which can handle strings and ints, the result is a string which you cannot assign to an int.

Another way (though not necessary easier or better) is:

String s = "2";
int i = Integer.valueOf(s).intValue();


in java 5 you can even drop the intValue() part:

String s = "2";
int i = Integer.valueOf(s);


With valueOf you can also get the Wrapper classes for primitive types if you need them.

If your number has a different base you can do it like this:
String s = "2A";
int i = Integer.parseInt(s,16);

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > String to int???


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT