JAVA将字符串CSV文件转换为double []数组

时间:2017-05-23 14:40:54

标签: java arrays csv double

我对CSV文件有疑问:这是该文件的示例。

4.98    24
9.14    21.6
4.03    34.7
2.94    33.4
5.33    36.2
5.21    28.7
12.43   22.9
19.15   27.1
29.93   16.5
17.1    18.9
20.45   15
13.27   18.9
15.71   21.7
8.26    20.4
10.26   18.2
8.47    19.9
6.58    23.1
14.67   17.5
11.69   20.2
11.28   18.2

目的是将其转换成类似的东西:

double[] column1= {4.98, 9.14, 4.03, 2.94, 5.33, 5.21, 12.43, 19.15}; //but with all the values, here it's just a sample
double [] column2 = {24, 21.6, 34.7, 33.4, 36.2, 28.7, 22.9, 27.1};

我不想写所有数据,我需要做一个循环,但我不知道我该怎么做。

我设法读取csv文件并拆分数据,但不将结果转换为double []数组。以下是我阅读文件的方法:

String fileName = "dataset.csv";
File file = new File(fileName);
String[] values;
try{
    Scanner inputStream = new Scanner(file);
    while (inputStream.hasNext()){
        String data = inputStream.next();
        values = data.split(",");
    }
    inputStream.close();

}
catch (FileNotFoundException e){
    e.printStackTrace();
}

2 个答案:

答案 0 :(得分:1)

您可以流式传输文件中的行并将其拆分为String[],就像您一样。然后,您可以流式传输每个此类数组并将每个项目转换为double,然后将它们收集回数组:

double[][] data =
    Files.lines(Paths.get(fileName))
         .map(s -> s.split(","))
         .map(s -> Arrays.stream(s).mapToDouble(Double::parseDouble).toArray())
         .toArray(double[][]::new);

答案 1 :(得分:0)

这是Java 7的完整工作示例:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Example
{
    public static void main(String args[])
    {
        Scanner inputStream = null;
        try
        {
            String fileName = "dataset.csv";
            File file = new File(fileName);

            // we don't know the amount of data ahead of time so we use lists
            List<Double> col1 = new ArrayList<>();
            List<Double> col2 = new ArrayList<>();

            inputStream = new Scanner(file);
            while (inputStream.hasNext())
            {
                String data = inputStream.next();
                String [] arr = data.split(",");

                col1.add(Double.parseDouble(arr[0]));
                col2.add(Double.parseDouble(arr[1]));
            }

            // Covert the lists to double arrays
            double[] column1 = new double[col1.size()];
            double[] column2 = new double[col2.size()];

            for (int i = 0; i < col1.size(); i++)
            {
                column1[i] = col1.get(i);
            }

            for (int i = 0; i < col2.size(); i++)
            {
                column2[i] = col2.get(i);
            }

            // print out just for verification
            System.out.println(Arrays.toString(column1));
            System.out.println(Arrays.toString(column2));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (inputStream != null)
            {
                inputStream.close();
            }
        }
    }
}