0

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) {

    }

}

}

1
  • Did you intend to leave the line trips.add(attributes2[1][9]) there? The only time you add something to trips is on that line, and attributes2 has nothing in it at that point. In fact, it looks like attributes2 never has anything stored in it. Commented May 3, 2016 at 23:01

1 Answer 1

1

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...
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting a NumberFormatException because some data cannot be parsed as a double. Exception in thread "main" java.lang.NumberFormatException: For input string: "4/7/2009" at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at Messin.main(Messin.java:47)
Then attributes should not be a double array... You can easily change it to a string array, and remove the parsing logic.
Thank you so much, I really appreciate you taking the time out to help me. I can't believe I couldn't notice something so simple!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.