简体   繁体   中英

Java : CSV reading with ArrayList of List of Integer

So, hello all, this is my first question here. Indeed after looking for a while I didn't find anything helpful. I am quite new to java and Data Mining. I have an issue, so I am trying to get some data from a CSV file to convert it to an ARFF (which will allow me to open it properly in Weka) So the point is that the CSV I have is kind of broken, I have 2 attributes one is emotions and one is pixels, but the fact is that the pixels one is made from 17 different pixel value. I was planning to put my firsts attribute in an ArrayList of List where each list is made of one element and represent one emotions. And another ArrayList of List where each list is a bunch of my 17 pixels I don't know if I am clear, but here is my code followed by my output and by what I want as output.

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);
    }}

So here is a shorten CSV I am using :

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 

(Please note that the CSV as I told you is kind of broken, it's the given one for my coursework and that's why I am trying to fix it first)

Finally by running this code for this CSV I have this as output :

[[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]]

While I want this :

[[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]]

I hope I am clear. I know that it's just because I am using the add method to a list, but I didn't manage to complete this task even by using the scanner reading line by line, or some other methods. And I also tried to clear the List before putting it again in the ArrayList but it juste give an totally EMPTY ArrayList.

Thank you very much for your help.

you have a list of lists

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

and you read your file into a list

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

and store it into the List of Lists

Emotions.add(emotions);

if you print that out you get:

[[0, 1, 3]]

if you want to have it different, you have to create your List<Integer> emotions for every loop/line you process, so eventually your fillArrayList method may look like this:

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM