comma-separated floats

B

bob

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 = ?
 
D

Daniel Pitts

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.
 
M

markspace

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.
 
R

Roedy Green

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.
 
L

Lew

A

Andreas Leitgeb

Lew said:
markspace said:
bob 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 = ?
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?
 
J

Jerry

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,841
Latest member
WilmerBelg

Latest Threads

Top