#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3

    Arrow (Java Newbie) Convert first few characters of string to double


    Let's say I have a string:

    Code:
    String s1 = "90:39.2";
    I would like to parse all the numbers up to ":" into a double, in this case 90.

    I can find the index of ":" by using:

    Code:
    int n = s1.indexOf(":");
    But I am not sure how to 'remove' the following numbers / characters before using this code to parse the string:

    Code:
    double result = Double.parseDouble(StringUpToColon);
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    Building on the code you have you'd want to do something like:

    java Code:
    String s1 = "90:39.2";
    int n = s1.indexOf(":");
     
    String firstPart = null;
    if( n > -1 )
        firstPart = s1.substring( 0, n );
    if( firstPart != null )
        double result = Double.parseDouble(firstPart);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Substring, that's what I needed. Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo