Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8940

Re: comma-separated floats

From Jerry <jerry.bowman@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: comma-separated floats
Date 2011-10-17 21:05 -0700
Organization http://groups.google.com
Message-ID <ff2e0cbb-b29e-482c-a5a5-b28086662545@f16g2000yqk.googlegroups.com> (permalink)
References <d27f3e79-81e1-4218-abcb-28b342033e08@dk6g2000vbb.googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar


Thread

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

csiph-web