2015-04-02 34 views
2

我想从csv文件中的单独列创建两个不同的数组。 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; 
    } 
}  
+0

你想阵列或数组列表(看你有一个温度数组列表)?无论何时您不知道如何使用标准的Java类,最好先参考[documentation](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList)的.html)。你会发现你可以在大部分时间回答你自己的问题。提示:ArrayList有一个.add()方法。 – MarsAtomic 2015-04-02 19:26:57

回答

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

只需将值添加到ArrayList中。

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 

你到底想要的是一个int数组monthsArr与价值{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

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

如何读取和解析CSV文件?我会使用第三方CSV阅读器说CSV阅读器。 “为什么不写自己的CSV解析器”列表可以找到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]); 
相关问题