Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #8493 > unrolled thread
| Started by | bob <bob@coolgroups.com> |
|---|---|
| First post | 2011-10-03 10:36 -0700 |
| Last post | 2011-10-17 21:05 -0700 |
| Articles | 8 — 7 participants |
Back to article view | Back to comp.lang.java.programmer
comma-separated floats bob <bob@coolgroups.com> - 2011-10-03 10:36 -0700
Re: comma-separated floats Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-03 10:49 -0700
Re: comma-separated floats markspace <-@.> - 2011-10-03 11:30 -0700
Re: comma-separated floats Lew <lewbloch@gmail.com> - 2011-10-04 08:40 -0700
Re: comma-separated floats Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-10-04 16:28 +0000
Re: comma-separated floats markspace <-@.> - 2011-10-04 10:44 -0700
Re: comma-separated floats Roedy Green <see_website@mindprod.com.invalid> - 2011-10-03 11:38 -0700
Re: comma-separated floats Jerry <jerry.bowman@gmail.com> - 2011-10-17 21:05 -0700
| From | bob <bob@coolgroups.com> |
|---|---|
| Date | 2011-10-03 10:36 -0700 |
| Subject | comma-separated floats |
| Message-ID | <d27f3e79-81e1-4218-abcb-28b342033e08@dk6g2000vbb.googlegroups.com> |
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 = ?
[toc] | [next] | [standalone]
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Date | 2011-10-03 10:49 -0700 |
| Message-ID | <3Bmiq.2435$pH6.1554@newsfe10.iad> |
| In reply to | #8493 |
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.
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-10-03 11:30 -0700 |
| Message-ID | <j6cuvn$2fk$1@dont-email.me> |
| In reply to | #8493 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-10-04 08:40 -0700 |
| Message-ID | <21508759.1902.1317742836345.JavaMail.geo-discussion-forums@prfp13> |
| In reply to | #8498 |
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
[toc] | [prev] | [next] | [standalone]
| From | Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> |
|---|---|
| Date | 2011-10-04 16:28 +0000 |
| Message-ID | <slrnj8md0j.6gl.avl@gamma.logic.tuwien.ac.at> |
| In reply to | #8527 |
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?
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-10-04 10:44 -0700 |
| Message-ID | <j6fgl9$f1o$1@dont-email.me> |
| In reply to | #8532 |
On 10/4/2011 9:28 AM, Andreas Leitgeb wrote: > Or did I miss something else? Likely it was that I spelled "delimiter" incorrectly.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-10-03 11:38 -0700 |
| Message-ID | <j50k87lccua3v12ufps274tqhhnetr9vl6@4ax.com> |
| In reply to | #8493 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Jerry <jerry.bowman@gmail.com> |
|---|---|
| Date | 2011-10-17 21:05 -0700 |
| Message-ID | <ff2e0cbb-b29e-482c-a5a5-b28086662545@f16g2000yqk.googlegroups.com> |
| In reply to | #8493 |
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.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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web