I have a CSV file each line formatted as: 20 char string, 10 char string, 4 char string, and a double.
The goal is to read the file and store the information into a list collection.
then sort using a comparator on the 3rd column and then the 1st column.
What I'd like to know is how to sort them, and whether or not there is an easier way of doing this. ideally the print would be spaced out with something like
System.out.printf("%-20s%-10s%-4s%10s%n", Result.get(0), Result.get(1), Result.get(2), Result.get(3));
Which I attempted, but could not display without needing the "System.out.println(CSVtoArrayList(Line));" line. my current code:
public static void main(String[] args) {
BufferedReader Buffer = null;
try {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter file name: ");
String input1 = scanner.nextLine();
String Line;
Buffer = new BufferedReader(new FileReader(input1));
while ((Line = Buffer.readLine()) != null) {
System.out.println(CSVtoArrayList(Line));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (Buffer != null) Buffer.close();
} catch (IOException Exception) {
Exception.printStackTrace();
}
}
}
public static ArrayList<String> CSVtoArrayList(String CSV) {
ArrayList<String> Result = new ArrayList<String>();
if (CSV != null) {
String[] splitData = CSV.split("\\s*,\\s*");
for (int i = 0; i < splitData.length; i++) {
if (!(splitData[i] == null) || !(splitData[i].length() == 0)) {
Result.add(splitData[i].trim());
}
}
}
return Result;
}
this currently prints out something like:
Enter file name:
comma.txt
[frist movei, Bob, 1999, 24.98]
[first mobie, Steve, 1999, -345.67]
[first movie, Pam, 1999, 0.00]
[other moive, Sam, 1562, -42.16]
[something, Sue, 8951, 224.62]