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) {
}
}
}
trips.add(attributes2[1][9])there? The only time you add something totripsis on that line, andattributes2has nothing in it at that point. In fact, it looks likeattributes2never has anything stored in it.