Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   comma-separated floats (http://www.velocityreviews.com/forums/t754604-comma-separated-floats.html)

bob 10-03-2011 05:36 PM

comma-separated floats
 
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 = ?

Daniel Pitts 10-03-2011 05:49 PM

Re: comma-separated floats
 
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.

markspace 10-03-2011 06:30 PM

Re: comma-separated floats
 
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.



Roedy Green 10-03-2011 06:38 PM

Re: comma-separated floats
 
On Mon, 3 Oct 2011 10:36:29 -0700 (PDT), bob <bob@coolgroups.com>
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.


Lew 10-04-2011 03:40 PM

Re: comma-separated floats
 
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

Andreas Leitgeb 10-04-2011 04:28 PM

Re: comma-separated floats
 
Lew <lewbloch@gmail.com> 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?


markspace 10-04-2011 05:44 PM

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



Likely it was that I spelled "delimiter" incorrectly.




Jerry 10-18-2011 04:05 AM

Re: comma-separated floats
 
On Oct 3, 12:36*pm, bob <b...@coolgroups.com> 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.ArrayConve rter;
import org.apache.commons.beanutils.converters.FloatConve rter;

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


All times are GMT. The time now is 07:10 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.