Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    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?

  • #2
    Regular Coder cs_student's Avatar
    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

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    24
    Thanks
    4
    Thanked 0 Times in 0 Posts
    why is it -48?

  • #4
    New Coder
    Join Date
    Aug 2009
    Location
    Cochin,India
    Posts
    39
    Thanks
    2
    Thanked 1 Time in 1 Post

    Smile

    Integer.parseInt(str) can be used parse string as integer

  • #5
    Regular Coder cs_student's Avatar
    Join Date
    Oct 2009
    Location
    ~/
    Posts
    195
    Thanks
    2
    Thanked 22 Times in 22 Posts
    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.

  • Users who have thanked cs_student for this post:

    BuhRock (12-01-2010)

  • #6
    New Coder
    Join Date
    Feb 2010
    Posts
    24
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Ok, yeah I got it, thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •