comma-separated floats

Discussion in 'Java' started by bob, Oct 3, 2011.

  1. bob

    bob Guest

    What is the easiest way to convert a bunch of comma-separated floats
    into float[]?

    String s =
    "5.792327,13.093715,-1.47686,15.495049,12.842123,-2.455942,1.326654,12.842123,0.000000";

    float[] f = ?
     
    bob, Oct 3, 2011
    #1
    1. Advertising

  2. bob

    Daniel Pitts Guest

    On 10/3/11 10:36 AM, bob wrote:
    > What is the easiest way to convert a bunch of comma-separated floats
    > into float[]?
    >
    > String s =
    > "5.792327,13.093715,-1.47686,15.495049,12.842123,-2.455942,1.326654,12.842123,0.000000";
    >
    > float[] f = ?


    Easiest is to use String.split(","), and then Float.parseFloat() on each
    value in the array returned from String.split(), storing it into the
    array you created and stored variable in f.

    Hope this helps, and good luck.
     
    Daniel Pitts, Oct 3, 2011
    #2
    1. Advertising

  3. bob

    markspace Guest

    On 10/3/2011 10:36 AM, bob wrote:
    > What is the easiest way to convert a bunch of comma-separated floats
    > into float[]?
    >
    > String s =
    > "5.792327,13.093715,-1.47686,15.495049,12.842123,-2.455942,1.326654,12.842123,0.000000";
    >
    > float[] f = ?



    Perhaps something like

    Scanner scanner = new Scanner( s );
    scanner.useDelimeter( "," );
    ArrayList<Float> floats = new ArrayList<Float>();

    while( scanner.hasNext() ) {
    floats.add( scanner.nextFloat() );
    }

    All from memory, not syntax checked or tested.
     
    markspace, Oct 3, 2011
    #3
  4. bob

    Roedy Green Guest

    On Mon, 3 Oct 2011 10:36:29 -0700 (PDT), bob <>
    wrote, quoted or indirectly quoted someone who said :

    >What is the easiest way to convert a bunch of comma-separated floats
    >into float[]?
    >
    >String s =
    >"5.792327,13.093715,-1.47686,15.495049,12.842123,-2.455942,1.326654,12.842123,0.000000";
    >
    >float[] f = ?


    You can read a file like that or a StringInputStream) with CSV. See
    http://mindprod.com/jgloss/csv.html

    You can also use Pattern split then parseFloat.
    see http://mindprod.com/jgloss/regex.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    It should not be considered an error when the user starts something
    already started or stops something already stopped. This applies
    to browsers, services, editors... It is inexcusable to
    punish the user by requiring some elaborate sequence to atone,
    e.g. open the task editor, find and kill some processes.
     
    Roedy Green, Oct 3, 2011
    #4
  5. bob

    Lew Guest

    markspace wrote:
    > bob wrote:
    >> What is the easiest way to convert a bunch of comma-separated floats
    >> into float[]?
    >>
    >> String s =
    >> "5.792327,13.093715,-1.47686,15.495049,12.842123,-2.455942,1.326654,12.842123,0.000000";
    >>
    >> float[] f = ?

    >
    > Perhaps something like
    >
    > Scanner scanner = new Scanner( s );
    > scanner.useDelimeter( "," );


    Uh-uh. 'scanner.useDelimiter( "," );'
    <http://download.oracle.com/javase/7/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)>

    --
    Lew
     
    Lew, Oct 4, 2011
    #5
  6. Lew <> wrote:
    > markspace wrote:
    >> bob wrote:
    >>> What is the easiest way to convert a bunch of comma-separated floats
    >>> into float[]?
    >>> String s =
    >>> "5.792327,13.093715,-1.47686,15.495049,12.842123,-2.455942,1.326654,12.842123,0.000000";
    >>> float[] f = ?

    >> Perhaps something like
    >> Scanner scanner = new Scanner( s );
    >> scanner.useDelimeter( "," );

    > Uh-uh. 'scanner.useDelimiter( "," );'
    ><http://download.oracle.com/javase/7/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)>


    I followed the Link, but couldn't find anything "Uh-oh"-worthy.

    Is the String-variant of it really that "Uh-oh"-ish?
    Isn't scanner.useDelimiter( Pattern.compile(",") ) very likely
    to be merely a case of pre-mature optimization to the cost of
    making the code a bit more complicated? Especially as it isn't
    even happening in the innermost loop...

    Or did I miss something else?
     
    Andreas Leitgeb, Oct 4, 2011
    #6
  7. bob

    markspace Guest

    On 10/4/2011 9:28 AM, Andreas Leitgeb wrote:
    > Or did I miss something else?



    Likely it was that I spelled "delimiter" incorrectly.
     
    markspace, Oct 4, 2011
    #7
  8. bob

    Jerry Guest

    On Oct 3, 12:36 pm, bob <> wrote:
    > What is the easiest way to convert a bunch of comma-separated floats
    > into float[]?
    >
    > String s =
    > "5.792327,13.093715,-1.47686,15.495049,12.842123,-2.455942,1.326654,12.8421 23,0.000000";
    >
    > float[] f = ?


    Use ArrayConverter from the Apache commons-beanutils package:

    import org.apache.commons.beanutils.converters.ArrayConverter;
    import org.apache.commons.beanutils.converters.FloatConverter;

    private void test() {
    String floatString = "1.0,3.14,2.7128";

    // The following two lines do all the work.
    ArrayConverter ac = new ArrayConverter(float[].class, new
    FloatConverter());
    float[] floats = (float[]) ac.convert(float[].class, floatString);

    // Verify parsing
    for (float f : floats) {
    System.out.println(f);
    }
    }

    Will print out:
    1.0
    3.14
    2.7128
     
    Jerry, Oct 18, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Miles
    Replies:
    1
    Views:
    606
    Jürgen Exner
    May 28, 2004
  2. Peter Rilling

    Array to a comma Separated String

    Peter Rilling, Jul 8, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    509
  3. DrLovely@.
    Replies:
    7
    Views:
    484
    DrLovely@.
    Mar 25, 2005
  4. RogerTBrick
    Replies:
    3
    Views:
    4,187
    RogerTBrick
    Mar 7, 2005
  5. =?Utf-8?B?Q2hyaXMgTGFuZQ==?=

    How to stream a comma separated string to the browser?

    =?Utf-8?B?Q2hyaXMgTGFuZQ==?=, Jul 21, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    354
    =?Utf-8?B?Q2hyaXMgTGFuZQ==?=
    Jul 21, 2006
Loading...

Share This Page