Results 1 to 4 of 4
  1. #1
    Xycose is offline Member
    Join Date
    Jun 2010
    Posts
    15
    Rep Power
    0

    Default Read in one linne into several doubles?

    * You will read a line of data with either 1 double with the value -1, 3 doubles, or 4 doubles
    * Numbers will be separate with a space, last number terminates with a newline
    So basically, the input is going to be one line, but it could be one double, two, three, or four doubles.

    How would I go about reading this input in? It would be easy if i knew there was going to always be 3 doubles, or if i knew there would always be four doubles. but how do i read one line into three doubles if three doubles are inputed into the one line, or only two doubles if only two doubles are inputted etc?

    Thanks for the help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A Scanner object can get doubles for you via the nextDouble method, and more importantly the hasNextDouble will tell you if there are any more double tokens available for scanning, and so you can count your doubles as you scan them in.

  3. #3
    Xycose is offline Member
    Join Date
    Jun 2010
    Posts
    15
    Rep Power
    0

    Default

    Thank you! hasNextDouble is what I needed. I didn't know about it.
    I knew how to scan them in.. Just couldn't think of a way to tell it to only scan in more if there are more.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're welcome. The other option is to use String#split(...) to split the String into tokens and then iterate through the resulting array, parsing to double with Double.parseDouble, as you go. If you did it this way, you may have to trim the Strings in the arrays before parsing.

Similar Threads

  1. Arithmetic using doubles
    By Black_Eye in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 08:35 AM
  2. arithmetic with doubles problem
    By hoosierfan24 in forum Java Software
    Replies: 1
    Last Post: 10-04-2010, 03:11 AM
  3. Cant use doubles
    By Freakzoyd in forum New To Java
    Replies: 27
    Last Post: 07-24-2010, 04:48 AM
  4. arrays using doubles [SOLVED]
    By soccer_kid_6 in forum New To Java
    Replies: 0
    Last Post: 03-10-2010, 01:56 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
  •