shawrie
shawrie

Reputation: 73

convert string data into double lists

Im trying a few things out with the AChart for android. Ive got a example working fine but want to change it slightly. Currently i have

List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 5230, 7300, 9240, 10540, 7900, 9200, 12030, 11200, 9500, 10500,
    11600, 13500 });

I want to populate it with my own data. I have a string variable that will be passed from a file.

String YTDData = "12.3,45,56.78,12,1,23.45"

How can i populate the values list with the contents of YTDData?

thanks

Upvotes: 1

Views: 1081

Answers (1)

JPM
JPM

Reputation: 9296

You can loop through the data and convert the string into an array and then to a double array.

    List<double[]> values = new ArrayList<double[]>();
    String YTDData = "12.3,45,56.78,12,1,23.45";

    String[] data = YTDData.split(",");

    double[] arrDouble = new double[data.length];

    for(int i=0; i<data.length; i++) {
       arrDouble[i] = Double.valueOf(data[i]);
    }
    values.add(arrDouble);

Upvotes: 3

Related Questions