Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2010, 01:13 AM   PM User | #1
BuhRock
New Coder

 
Join Date: Feb 2010
Posts: 24
Thanks: 4
Thanked 0 Times in 0 Posts
BuhRock is an unknown quantity at this point
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?
BuhRock is offline   Reply With Quote
Old 12-01-2010, 02:13 AM   PM User | #2
cs_student
Regular Coder

 
cs_student's Avatar
 
Join Date: Oct 2009
Location: ~/
Posts: 195
Thanks: 2
Thanked 22 Times in 22 Posts
cs_student is an unknown quantity at this point
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
cs_student is offline   Reply With Quote
Old 12-01-2010, 03:34 AM   PM User | #3
BuhRock
New Coder

 
Join Date: Feb 2010
Posts: 24
Thanks: 4
Thanked 0 Times in 0 Posts
BuhRock is an unknown quantity at this point
why is it -48?
BuhRock is offline   Reply With Quote
Old 12-01-2010, 04: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
sujithpr is an unknown quantity at this point
Smile

Integer.parseInt(str) can be used parse string as integer
sujithpr is offline   Reply With Quote
Old 12-01-2010, 05:20 AM   PM User | #5
cs_student
Regular Coder

 
cs_student's Avatar
 
Join Date: Oct 2009
Location: ~/
Posts: 195
Thanks: 2
Thanked 22 Times in 22 Posts
cs_student is an unknown quantity at this point
Quote:
Originally Posted by BuhRock View Post
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
cs_student is offline   Reply With Quote
Users who have thanked cs_student for this post:
BuhRock (12-01-2010)
Old 12-01-2010, 05:40 AM   PM User | #6
BuhRock
New Coder

 
Join Date: Feb 2010
Posts: 24
Thanks: 4
Thanked 0 Times in 0 Posts
BuhRock is an unknown quantity at this point
Ok, yeah I got it, thanks.
BuhRock is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:44 PM.


Advertisement
Log in to turn off these ads.