0

I have to retrieve some data from a file to show it in a chart. The function that shows the chart requires he data as float[] whereas the retrieved data is in the form ArrayList<String>.

What is the easiest way to convert ArrayList<String> to float[]?

        try {
            FileInputStream fIn = context.openFileInput(fileDir+fileName);
            InputStreamReader ipsr = new InputStreamReader(fIn);
            BufferedReader b = new BufferedReader(ipsr);

            ArrayList<String> list_prix = new ArrayList<String>();
            String ligne;

            while ((ligne = b.readLine()) != null) {
                String dtVal = ligne.split(" ")[2];
                dtVal = dtVal.substring(0, dtVal.length() - 2);
                list_prix.add(dtVal);
            }

            //just here if i can convert list_prix to float[]

            fIn.close();
            ipsr.close();
        } 
        catch (Exception e) 
        {
            Log.e("blah", "Exception", e);
        }

Thank you for your help.

4
  • 2
    What have you tried? Commented Sep 11, 2011 at 16:42
  • @Charles Goodwin: Please see my edited post. Commented Sep 11, 2011 at 16:45
  • do you need list_prix after the while loop Commented Sep 11, 2011 at 16:47
  • @Nammari:Yes,exactly. After the while->loop. Commented Sep 11, 2011 at 16:49

2 Answers 2

3

I think the following will do it using Guava...

Collection<Float> floats = Collections2.transform(list_prix, new Function<String, Float>() {
    public Float apply(String input) {
        return Float.parseFloat(input);
    }

});

Float[] floatArray = new Float[floats.size()];
floats.toArray(floatArray);
Sign up to request clarification or add additional context in comments.

2 Comments

IMHO, installing Guava just for that would be like killing a fly with a nuclear bomb.
Agree with @Cristian but I voted it up as an interesting alternative :-)
2

You can loop and use Float.parseFloat().

float [] floatValues = new float[list_prix.size()];

for (int i = 0; i < list_prix.size(); i++) {
    floatValues[i] = Float.parseFloat(list_prix.get(i));
}

Now, this assumes that every string in your ArrayList can actually be parsed into a float. If not, it could throw an exception, so you may want to do this in a try/catch block if you are not sure.

5 Comments

I would classify this answer as "hand holding" but nevermind.
And please how to convert arraylist<string> to string[]. Looking for your response please. I've tried to change the above code to change it to String but have always an error.
for (int i = 0; i < list_date.size(); i++) { list_date.toArray(dateValues);
Well, the string version would look pretty similar: stringValues[i] = list_prix.get(i); This works since list_prix is an arraylist of strings (defined by ArrayList<String>)
@Charles Goodwin: I guess it is a bit of hand holding, but we've all been there before. A post like this might help other newbies that may not know about "parseFloat", "parseInt", etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.