2017-11-03 62 views
1

所以,大家好,這是我的第一個問題。事實上,在找了一段時間後,我沒有找到有用的東西。我對Java和數據挖掘頗爲陌生。 我有一個問題,所以我想從CSV文件中獲取一些數據以將其轉換爲ARFF(這將允許我在Weka中正確打開它) 所以問題是我擁有的CSV有點破,我有2個屬性,一個是情緒,一個是像素,但事實是像素是由17個不同的像素值構成的。 我打算把我的第一個屬性放在List的ArrayList中,每個列表都由一個元素組成,代表一種情緒。 另一個列表的列表,其中每個列表是我的17個像素的一堆我不知道我是否清楚,但這裏是我的代碼後跟我的輸出和我想要的輸出。Java:使用ArrayList的Integer列表CSV讀取

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.nio.charset.Charset; 
import java.nio.file.Files; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

public class CSVformat { 

    private static ArrayList<List<Integer>> Emotions = new ArrayList<List<Integer>>(); 
    private static ArrayList<List<Integer>> Pixels = new ArrayList<List<Integer>>(); 

    public CSVformat(ArrayList<List<Integer>> emotions, ArrayList<List<Integer>> pixels){ 
    this.setEmotions(emotions); 
    this.setPixels(pixels); 
    } 

    /** 
    * @return the emotions 
    */ 
    public ArrayList<List<Integer>> getEmotions() { 
     return Emotions; 
    } 

    /** 
    * @param emotions the emotions to set 
    */ 
    public void setEmotions(ArrayList<List<Integer>> emotions) { 
     CSVformat.Emotions = emotions; 
    } 

    /** 
    * @return the pixels 
    */ 
    public ArrayList<List<Integer>> getPixels() { 
     return Pixels; 
    } 

    /** 
    * @param pixels the pixels to set 
    */ 
    public void setPixels(ArrayList<List<Integer>> pixels) { 
     CSVformat.Pixels = pixels; 
    } 

    /*public void readData(String fileName) throws IOException { 
     int count = 0; 
     String file = fileName; 
     try { 
      BufferedReader br = new BufferedReader(new FileReader(file)); 
      String line = ""; 
      while ((line = br.readLine()) != null) { 

       Emotions.add(line.split(",")); 

       String[][] v = (String[][]) bank.toArray(new String[bank.size()][12]); 

      } 
     } catch (FileNotFoundException e) { 

     } 
    }*/ 

    public static void fillArrayList(String fileName) throws FileNotFoundException { 
     List<String> list = new ArrayList<String>(); 
     List<Integer> emotions = new ArrayList<Integer>(); 
     List<Integer> pixels = new ArrayList<Integer>(); 

     File file = new File(fileName); 
     //Scanner scan = new Scanner(new File(fileName)); 
     if(file.exists()){ 
      try { 
       list = Files.readAllLines(file.toPath(),Charset.defaultCharset()); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
      if(list.isEmpty()) 
       //scan.close(); 
       return; 
     } 
     for(String line : list.subList(1, list.size())){ 
     String[] res = line.split(","); 
     String[] arr= res[1].split(" "); 
     emotions.add(Integer.parseInt(res[0])); 
     for(int i=0;i<arr.length;i++){ 
      pixels.add(Integer.parseInt(arr[i])); 

      } 

     } 
    Emotions.add(emotions); 
    Pixels.add(pixels); 
    System.out.println(Emotions); 
    System.out.println(Pixels); 
    }} 

因此,這裏是我使用的是縮短CSV:

emotion,pixels 
0,70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115 
1,70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115 
3,70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115 

(請注意,CSV,我告訴你的是那種壞了,這是給定一個我的課程,這就是爲什麼我我想先解決它)

最後通過運行該代碼,這個CSV我有這樣的輸出:

[[0, 1, 3]] 
[[70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115, 70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115, 70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115]] 

雖然我想要:

[[0], [1], [3]] 
[[70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115], [70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115], [70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115]] 

我希望我很清楚。 我知道這只是因爲我使用add方法列表,但我沒有設法完成此任務,甚至通過使用掃描儀逐行閱讀或其他一些方法。而且我還試圖在將它重新放入ArrayList之前清除列表,但它恰好給出了完全空的ArrayList。

非常感謝您的幫助。

+0

爲什麼你被解析自己呢?使用OpenCSV庫。 – m0skit0

+0

嗨m0skit0, 這不是這個任務的目的,我必須避免使用它,如果我可以的話。 –

回答

3

你有名單

private static ArrayList<List<Integer>> Emotions = new ArrayList<List<Integer>>(); 

的列表,你讀你的文件到列表

List<Integer> emotions = new ArrayList<Integer>(); 

並將其存儲到列表清單

Emotions.add(emotions); 

,如果你的打印你會得到:

[[0, 1, 3]] 

,如果你想擁有它不同,你必須創建你的List<Integer> emotions爲每個環路/線路您處理,所以最終你fillArrayList方法可能如下:

public static void fillArrayList(String fileName) throws FileNotFoundException { 
    List<String> list = new ArrayList<String>(); 
    File file = new File(fileName); 
    if (file.exists()) { 
     try { 
      list = Files.readAllLines(file.toPath(), Charset.defaultCharset()); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
     if (list.isEmpty()) 
      return; 
    } 

    for (String line : list.subList(1, list.size())) { 
     System.out.println(line); 

     String[] res = line.split(","); 
     String[] arr = res[1].split(" "); 

     List<Integer> emotions = new ArrayList<Integer>(); 
     emotions.add(Integer.parseInt(res[0])); 
     Emotions.add(emotions); 

     List<Integer> pixels = new ArrayList<Integer>(); 
     for (int i = 0; i < arr.length; i++) { 
      pixels.add(Integer.parseInt(arr[i])); 
     } 
     Pixels.add(pixels); 
    } 
} 
+0

非常感謝,我現在看起來很清楚,而且我感覺有點愚蠢,以至於我以前沒有看到它。 謝謝你們倆:) –