Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: string to double

  1. #1
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default string to double

    here's a snipit of code that i have , . what do i need to add to make this work

    weight is a double while data[2] is read from a txt file ( yes its a double in the txt file )
    weight = data[2];
    Programming: the art that fights back


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: string to double

    Primitive conversion from String to double can be done using the static method on the Double wrapper class. There are two of them which will parse the string into a either a Double wrapper object or a double primitive.

        String stringDouble = "123.456";
        Double wrapperDouble = Double.valueOf(stringDouble); // This gives you a return type of Double wrapper object
        double primitiveDouble = Double.parseDouble(stringDouble); // This give you a return type of the double primitive

    Also note that if you try to parse a string into any number but the string has invalid characters such as "123.4g56" the static method will throw a NumberFormatException.

    Exception in thread "main" java.lang.NumberFormatException: For input string: "123.4g56"
    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    wolfgar (October 13th, 2009)

  4. #3
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: string to double

    thanks , that helps alot
    Programming: the art that fights back

Similar Threads

  1. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  2. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  3. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  4. String and int problem in swing program
    By duckman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2009, 03:28 AM