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

Java 如何将.csv文件中的两个不同列转换为数组?,java,arrays,csv,Java,Arrays,Csv,我正在尝试从csv文件中的不同列创建两个不同的数组。 csv文件有一列表示“月份”,一列表示“温度”。这是我到目前为止所拥有的,但我不知道如何将这些值转换为数组 public class InputOutput { private double temperature; public InputOutput() { List<String> temperatures = new ArrayList<String>();

我正在尝试从csv文件中的不同列创建两个不同的数组。 csv文件有一列表示“月份”,一列表示“温度”。这是我到目前为止所拥有的,但我不知道如何将这些值转换为数组

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;
    }
}       
公共类输入输出
{
私人双温;
公共输入输出()
{
列表温度=新的ArrayList();
迭代器tempIterator=温度。迭代器();
}
公共静态双getTemp()
{
双温读数=0.0;
尝试
{
String fileName=“C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv”;//MNMINPLS.csv的位置
文件=新文件(文件名);
扫描仪扫描=新扫描仪(文件);//扫描仪读取文件
while(scan.hasNext())//迭代器
{
字符串tempData=scan.next();
字符串[]nums=tempData.split(“,”);
tempReading=Double.parseDouble(nums[3]);
系统输出打印LN(临时读取);
}//结束时
scan.close();
}
catch(filenotfounde异常)
{
e、 printStackTrace();
}//结束尝试/接球
回读;
}//结束方法getTemp
公共静态int getMonth()
{
int m=0;
尝试
{
String fileName=“C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv”;//MNMINPLS.csv的位置
文件=新文件(文件名);
扫描仪扫描=新扫描仪(文件);//扫描仪读取文件
while(scan.hasNext())//迭代器
{
String month=scan.next();
如果(月==月)
m++;
字符串[]timeOfYear=month.split(“,”);
m=整数.parseInt(timeOfYear[0]);
系统输出打印项次(m);
}
scan.close();
}
catch(filenotfounde异常)
{
e、 printStackTrace();
}//结束尝试/接球
返回m;
}
}       

只需将该值添加到ArrayList。

如果我理解正确,您的
,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
最后你需要的是一个int数组
monthsArr
,值为
{4,5,6}
,加上一个并行双数组
tempsArr
,值为
{70.3,74.5,85.8}

首先,将您的
temperatures
ArrayList设置为该类的成员(将其移动到
private double temperature
所在的位置),并添加一个额外的
months
ArrayList将温度设置为
ArrayList
并将月份设置为
ArrayList

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

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

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

ArrayList
API规范(javase7)是您只需读取一次文件

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++
          }

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

导入com.opencsv.CSVReader;
导入java.io.FileReader;
导入java.util.List;
导入java.util.ArrayList;
CSVReader读取器=新CSVReader(
新的文件阅读器(“C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv”);
字符串[]下一行;
//如果你想要列表
列表月份=新建ArrayList();
列表温度=新的ArrayList();
while(null!=(nextLine=reader.readNext())
{
月数。添加(
整数值(下一行[0]);
温度。添加(
Double.valueOf(下一行[1]);
}
//或者,如果希望将数据作为数组,请参见http://docs.oracle.com/javase/7/docs/api/java/util/List.html#toArray(T[]
整数[]monthsArr=months.toArray(新整数[0]);
Double[]tempsArr=温度。toArray(新的双精度[0]);

您想要阵列或阵列列表(查看温度的阵列列表)?当您不知道如何使用标准Java类时,最好先咨询。你会发现这样做后,你大部分时间都能回答自己的问题。提示:ArrayList有一个.add()方法。
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++
          }
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]);