The requirements within your question are rather confusing and things don't get any easier if there is no example CSV data. The confusing portion is in relation to Column 1 and Column 2 data regarding uniqueness. What do you consider to be UNIQUE?
A) Is it to be Unique in the sense that Column 1 and or Column 2 contains a string or numerical value which is not duplicated in any other CSV file line (no duplicate within its respective column)?
B) Or is it to be Unique in the sense that Column 1 and or Column 2 contains a string or numerical value which is unique upon itself and can be found in other CSV file lines (duplicates allowed within its respective column)?
The example code below assumes unique condition (A). This means then, if a CSV file contained the following comma delimited lines then only two of those lines would fulfill the specified data conditions:
Jack,Flash,yes,14,Unknown Value
Bob,Stick,no,11,Unknown Value
Jack,Flash,no,22,Unknown Value
Fred,Frog,yes,6,Unknown Value
Bob,Stick,no,32,Unknown Value
Tracey,Jones,no,17,Unknown Value
Fred,Frog,no,23,Unknown Value
John,Brown,no,12,Unknown Value
Bob,Stick,yes,88,Unknown Value
since only those two lines have columns 1 and 2 which are truly unique to the entire CSV file. Can you see which lines they are?
Here is the example code:
ArrayList<String> resultList = new ArrayList<>();
ArrayList<String> linesList = new ArrayList<>();
// Input of file which needs to be parsed
String csvFile = "sample.csv";
BufferedReader csvReader;
// Data split by ',' in CSV file
String csvSplitBy = ",";
try {
// Read the CSV file into an ArrayList array for easy processing.
String line;
csvReader = new BufferedReader(new FileReader(csvFile));
while ((line = csvReader.readLine()) !=null) {
linesList.add(line);
}
csvReader.close();
}
catch (IOException e) { e.printStackTrace(); }
// Process each CSV file line which is now contained within
// the linesList list Array
for (int i = 0; i < linesList.size(); i++) {
String[] data = linesList.get(i).split(csvSplitBy);
String col1 = data[0];
String col2 = data[1];
String col3YesNo = data[2];
//int col4Value = Integer.parseInt(data[3]); //WAS THIS
double col4Value = Double.parseDouble(data[3]); // *** SHOULD BE ***
String col5Unknown = data[4];
// Determine if Column 1 and Column 2 data for the
// current line is unique to the entire CSV file.
boolean columns1And2AreUnique = true;
for (int j = 0; j < linesList.size(); j++) {
String[] tmp = linesList.get(j).split(csvSplitBy);
// Make sure we don't process the same line we are on...
if (j != i) {
if (col1.equals(tmp[0]) || col2.equals(tmp[1])) {
columns1And2AreUnique = false;
break;
}
}
}
if (columns1And2AreUnique && col3YesNo.equalsIgnoreCase("no") && col4Value >= 12.0) {
resultList.add(linesList.get(i));
}
}
// Display the determined results from the CSV file.
if (resultList.isEmpty()) {
System.out.println("There could be no data results gathered from the supplied\n"
+ "CSV file which meets the required criteria.");
}
else {
System.out.println("Column 1\tColumn 2\tColumn 3\tColumn 4\tColumn 5");
System.out.println("================================================"
+ "========================\n");
String padString = " "; //Used for simple space padding in display
for (int i = 0; i < resultList.size(); i++) {
String[] tmp = resultList.get(i).split(csvSplitBy);
System.out.println(tmp[0] + padString.substring(tmp[0].length()) + "\t"
+ tmp[1] + padString.substring(tmp[1].length()) + "\t"
+ tmp[2] + padString.substring(tmp[2].length()) + "\t"
+ tmp[3] + padString.substring(tmp[3].length()) + "\t"
+ tmp[4]);
}
}
EDIT: Now that you have posted some sample data from your CSV file...
Well, I was very close with my columnar CSV Data Type assumptions but there does need to be a code change since I now know that the 4th data column contains Double data type values.
You will need to change the code line that states:
int col4Value = Integer.parseInt(data[3]);
to this code line which handles the Double data type values:
double col4Value = Double.parseDouble(data[3]);
This should help you a little while you are modifying the code to suit your needs.