Faith Matthew
Faith Matthew

Reputation: 3

Arraylist with data from CSV file is full of null values?

For a question I have to find the number of unique Strings in a column and write it into a CSV file.

My plan was to put the first String inside an Arraylist and loop through the column and add any strings not within the Arraylist

It works with any ordinary Arraylist but for some reason the Arraylist containing data from the CSV file is all null

Can anyone explain to me why this is and how I can fix it? My code is below.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

public class uniqueTrips {
static ArrayList<String> removeDuplicates(ArrayList<String> list) {

    ArrayList<String> result = new ArrayList<>();

    HashSet<String> set = new HashSet<>();

    for (String item : list) {

        if (!set.contains(item)) {
            result.add(item);
            set.add(item);
        }
    }
    return result;
}

public static void main(String[] args) throws IOException {

    ArrayList<String> trips = new ArrayList<String>();
    try {
        BufferedReader reader = new BufferedReader(new FileReader("Passenger_Weather_Combined.csv"));
        BufferedWriter writer = new BufferedWriter(new FileWriter("result.csv"));
        double[] attribute = new double[15];
        double[][] attributes = new double[77284][15];
        String[][] attributes2 = new String[77284][15];
        String line = reader.readLine();
        int number = 0; // Rows!

        trips.add(attributes2[1][9]);

        while (line != null) {
            String[] att = line.split(",");
            attributes[number] = attribute;
            line = reader.readLine();
        }
        System.out.println(removeDuplicates(trips).size());

        writer.newLine();
        number++;

        writer.close();
        reader.close();
    } catch (IOException e) {

    }

}

}

Upvotes: 0

Views: 874

Answers (1)

Andrew Williamson
Andrew Williamson

Reputation: 8691

This bit here is where you iterate through the rows of the file:

while (line != null) {
    String[] att = line.split(",");
    attributes[number] = attribute;
    line = reader.readLine();
}

However, you are not actually touching the data you read in. You read the row into line, you split the line into columns, and save it in att. Then you never use the value in att from then on - instead, you use the variable attribute, which doesn't contain any meaningful data. Try changing the loop to this:

while (line != null) {
    String[] att = line.split(",");

    attributes[number] = new double[att.length];
    for (int i = 0; i < att.length; i++)
        attributes[number][i] = Double.parseDouble(att[i]);

    line = reader.readLine();
    number++; // Very important, otherwise you're always updating the same row...
}

Upvotes: 1

Related Questions