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 4 of 4

Thread: String to Double Conversion

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default String to Double Conversion

    I'm sure this is something very simple, but after having spent about two hours I have run out of ideas. A very simple program creating a array and having a user input 10 numbers via a dialog box. Then taking those numbers and displaying them smallest to largest. It worked fine when I had the variables set as Integers, but know that I have changed everything to Double, I can't convert the user input from String to Double. I keep getting the error "Loss of precision" "Required int, found double" . All variables and the list are Double, but something is being read as an Integer somewhere, somehow. Any recommendations?


    double list [] = new double [10];

    for (double num = 0.0; num < list.length ; num++){
    String numStr = JOptionPane.showInputDialog("Enter a number");
    list [num] = Double.parseDouble(numStr);
    }

    Arrays.sort(list);
    for (double x = 0.0; x < list.length; x++){
    }

    final String numbersList = Arrays.toString( list );
    JOptionPane.showMessageDialog( null, "Your numbers are: " + numbersList );
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: String to Double Conversion

    Please post your code in formatting tags. You can learn how in the Announcments at the top of the page.

    It's not clear why you'd be getting that error with the code you've posted. It's also not clear why you changed every int to a double, especially those used as for loop control variables which are then used as array indices. doubles cannot be used as array indices, so that IS a real error you should be getting.

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

    suteki73 (September 21st, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String to Double Conversion

    Thanks. I changed the for loop control variables back to Integers and it works now. Don't really understand why that caused the "list [num] = Double.parseDouble(numStr);" line to highlight as an error though, but the important thing is it works now. Thanks again.

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: String to Double Conversion

    Quote Originally Posted by suteki73 View Post
    Thanks. I changed the for loop control variables back to Integers and it works now. Don't really understand why that caused the "list [num] = Double.parseDouble(numStr);" line to highlight as an error though
    Think about it this way:
    If the variable num is a double, then:
    num = 1.3;
    list[num]
    exactly what index is 1.3?
    That is why it must be an int and not a double

Similar Threads

  1. Replies: 9
    Last Post: August 30th, 2012, 03:25 PM
  2. Need help regarding String to int Conversion
    By fredsilvester93 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 20th, 2012, 04:39 PM
  3. String and Bit Array conversion issues
    By GeekWarth in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 07:13 PM
  4. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  5. [SOLVED] utf-16 byte[] to string conversion
    By Gerhardl in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 25th, 2010, 07:06 AM

Tags for this Thread