2015-04-02 65 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]);