如何将.csv文件中的两个不同列转换为数组?

时间:2015-04-02 19:18:23

标签: java arrays csv

我正在尝试从csv文件中的不同列创建两个不同的数组。 csv文件有一列用于' month' s'温度为'一列。这是我到目前为止所做的,但我无法弄清楚如何将这些值转换为数组。

public class InputOutput
{
    private double temperature;

    public InputOutput()
    {
        List<String> temperatures = new ArrayList<String>();
        Iterator<String> tempIterator = temperatures.iterator();

    }

    public static double getTemp()
    {
        double tempReading = 0.0;
        try
        {
            String fileName = "C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv";                 //location of MNMINPLS.csv
            File file = new File(fileName);
            Scanner scan = new Scanner(file);                                               //scanner reads file
            while(scan.hasNext())                           //iterator
            {
                String tempData = scan.next();
                String[] nums = tempData.split(",");
                tempReading = Double.parseDouble(nums[3]);

                System.out.println(tempReading);
            }//end while
            scan.close();
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }//end try/catch
        return tempReading;
    }//end method getTemp

    public static int getMonth() 
    {
        int m = 0;
        try
        {
            String fileName = "C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv";                 //location of MNMINPLS.csv
            File file = new File(fileName);
            Scanner scan = new Scanner(file);                                               //scanner reads file
            while(scan.hasNext())                       //iterator
            {
                String month = scan.next();
                    if(month == month)
                        m++;
                String[] timeOfYear = month.split(",");
                m = Integer.parseInt(timeOfYear[0]);

                System.out.println(m);
            }
            scan.close();
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }//end try/catch
        return m;
    }
}       

4 个答案:

答案 0 :(得分:0)

temperatures.add(Double.toString(tempReading));

只需将值添加到ArrayList。

答案 1 :(得分:0)

如果我理解正确,您有<date>,temp行,如下所示:

4,12,2014,70.3 //12 Apr 2014 weather
5,1,2014,74.5 //1 May 2014 weather
6,27,2014,85.8 //27 June 2014 weather

最终你想要的是一个值为monthsArr的int数组{4, 5, 6},以及一个值为tempsArr

的并行双数组{70.3, 74.5, 85.8}

首先,让你的temperatures ArrayList成为类的成员(将其移动到private double temperature所在的位置),然后添加一个额外的months ArrayList。 将温度设为ArrayList<Double>,将温度设为ArrayList<Integer>

然后,您可以使用add()方法将解析后的值添加到相应的ArrayList中。

最后,完成解析后,转换为数组的最简单方法是使用toArray()方法,如下所示:

double[] tempsArr = temperatures.toArray(new double[]);
int[] monthsArr = months.toArray(new int[]);

ArrayList<E> API规范(Java SE7)为here

答案 2 :(得分:0)

您只需要阅读一次文件

double[] temperatures=new double[maxsize];
 String[] months=new String[maxsize];
 int index=0;
    while(scan.hasNext())                           //iterator
            {
                String tempData = scan.next();
                String[] nums = tempData.split(",");
                tempReading = Double.parseDouble(nums[3]);
                monthReading = nums[2]; //or whichever index
                temperatures[index]=tempReading;
                months[index]=monthReading;
                index++
          }

答案 3 :(得分:0)

如何阅读和解析CSV文件?我会使用第三方CSV阅读器说CSV阅读器。 &#34;为什么不编写自己的CSV解析器&#34;列表可以找到here。希望这会有所帮助:):

import com.opencsv.CSVReader;

import java.io.FileReader;
import java.util.List;
import java.util.ArrayList;

CSVReader reader = new CSVReader(
                        new FileReader("C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv"));
String [] nextLine;

// If you want lists
List<Integer> months       = new ArrayList<Integer>();
List<Double>  temperatures = new ArrayList<Double>();
while (null != (nextLine = reader.readNext())) 
{
    months.add(
                Integer.valueOf(nextLine[0]));
    temperatures.add(
                Double.valueOf(nextLine[1]));
}

// or if you want your data as an array see http://docs.oracle.com/javase/7/docs/api/java/util/List.html#toArray(T[])
Integer[] monthsArr = months.toArray(new Integer[0]);
Double[]  tempsArr  = temperatures.toArray(new Double[0]);