Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Convert String to Integer'S

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 12-01-2010, 02:13 AM PM User | #1 BuhRock New Coder   Join Date: Feb 2010 Posts: 24 Thanks: 4 Thanked 0 Times in 0 Posts Convert String to Integer'S How would I go about converting a string to an integer? Say someone enters a 4 digit string. Like 1234. Could I use the place of each digit and work with it? Pretty much, I wanna get 1 from the string "1234" and put it in a variable. Then get 2 from the string "1234" and put it in its own variable, and so forth. Like take the 0 spot, which would be 1. and say change it to something else? I know I could do something like Code: ```public class GetIntFromString { public static void main(String[] args) { String str = "1234"; for(int i = 0; i < str.length(); i++) { str.charAt(i); System.out.println(i); } } }``` Also do the same thing for each digit of the string. Is this possible without using an array?
 12-01-2010, 03:13 AM PM User | #2 cs_student Regular Coder     Join Date: Oct 2009 Location: ~/ Posts: 195 Thanks: 2 Thanked 22 Times in 22 Posts There are actually a couple of ways you could go about doing this. One way would be to use the Integer.getInteger(String nm) method. The other would be to get the characters as you are doing above, casting them to an int, then subtracting the corresponding amount to get them to the number you want to be. For example Code: ```char num = '0'; // should print out 0 System.out.println(((int) num)-48)); // or you can do int foo = ((int) num) - 48;``` Hope that helps __________________ Get GNU/Linux - Play Ogg - Vim Using Arch Linux x86_64, Xorg + xmonad
 12-01-2010, 04:34 AM PM User | #3 BuhRock New Coder   Join Date: Feb 2010 Posts: 24 Thanks: 4 Thanked 0 Times in 0 Posts why is it -48?
 12-01-2010, 05:40 AM PM User | #4 sujithpr New Coder   Join Date: Aug 2009 Location: Cochin,India Posts: 39 Thanks: 2 Thanked 1 Time in 1 Post Integer.parseInt(str) can be used parse string as integer
12-01-2010, 06:20 AM   PM User | #5
cs_student
Regular Coder

Join Date: Oct 2009
Location: ~/
Posts: 195
Thanks: 2
Thanked 22 Times in 22 Posts
Quote:
 Originally Posted by BuhRock why is it -48?
We do that to account for the account for the offset of the character in the ascii (or unicode) encoding scheme. To the computer the number 48 and the character 0 are the same. They both contain the same binary number, you simply just tell the computer that one is a character and the other is a number. So in that code I am simply telling the computer to treat the character '0' as a number instead of a character. Once we do this the computer will see the variable as 48. Thus we subtract 48 so that we will have the correct numerical number, 0. Since '1' is equivalent to 49, '2' to 50 ... etc. We can simply cast the character to an integer and subtract 48 to get the correct value.

I hope that made since. For a more visual understanding look up ascii tables, as they will show you the entire ascii encoding scheme.
__________________
Get GNU/Linux - Play Ogg - Vim
Using Arch Linux x86_64, Xorg + xmonad

 Users who have thanked cs_student for this post: BuhRock (12-01-2010)
 12-01-2010, 06:40 AM PM User | #6 BuhRock New Coder   Join Date: Feb 2010 Posts: 24 Thanks: 4 Thanked 0 Times in 0 Posts Ok, yeah I got it, thanks.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 11:59 PM.