簡體   English   中英

JAVA將字符串CSV文件轉換為double []數組

[英]JAVA convert string CSV file into double[] array

我的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();
}

您可以像執行流操作一樣,從文件中傳輸行並將其拆分以獲取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);

這是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();
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM