Results 1 to 6 of 6
  1. #1
    gabriel is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default how to convert String number to int

    I want to convert from String numbers "12345"; to five separate ints
    how can I do that?
    thanks

  2. #2
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    Integer.parseInt() takes a String and turns it into a number.

    To get each character use the String.toCharArray();

    For example you can do the following:
    Java Code:
    String num = "12345";
    char[] arr = num.toCharArray();
    
    for (char n : arr)
    	{
    	int i = Integer.parseInt(n + "");
    	print(i);
    	}
    BTW the n+"" is a shortcut to turn n into a String.

  3. #3
    gabriel is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default

    very well
    thanks I know that was a silly question
    sorry

  4. #4
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    One of our friend have made this simple explanation bout Strings in Java if u need some added reference besides the one from sun, here's the link
    http://www.java-forums.org/java-book...va-string.html

  5. #5
    Sundhas is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Default

    Hey i have a question
    Authentication A1 = new Authentication(1);
    System.out.println("Enter ID");

    A1.SetID(Integer.parseInt(System.in.read()));
    System.out.println(A1.GetID());

    Authentication is the name of Class i made
    Problem is when i dont use Interger.parseInt if i enter value 1 it prints it 50
    and if i convert that to int it gives me error :S

  6. #6
    soheil is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

Similar Threads

  1. How to convert a string into a BigInteger
    By valery in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 01:32 PM
  2. Replies: 0
    Last Post: 04-06-2008, 07:40 PM
  3. Replies: 0
    Last Post: 03-01-2008, 10:01 PM
  4. convert string to float
    By miss_dot in forum NetBeans
    Replies: 1
    Last Post: 11-14-2007, 11:26 PM
  5. Help with convert a double type number
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:48 AM

Posting Permissions

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