你的問題中的要求是相當混亂,事情不要」如果沒有示例CSV數據,就會變得更輕鬆。令人困惑的部分是關於唯一性的第1欄和第2欄數據。你認爲什麼是UNIQUE?
A)是它是在這個意義上獨特之處在於第1列和或第2列含有未在任何其他CSV文件中的行重複的字符串或數值(其各自的列內沒有重複的)?
B)或者是它是在這個意義上,列1和或第2列包含一個字符串或數值的值,該值是在其自身獨特的,並且可以在其他CSV文件行中找到(允許內重複獨特其各列)?
下面的示例代碼假定爲唯一條件(A)。那麼這意味着,如果一個CSV文件含有如下逗號分隔的行然後只有兩個那些線將實現指定的數據的條件:
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
因爲只有那些兩行具有真正獨特的列1和2整個CSV文件。你能看到他們是哪條線嗎?
下面是示例代碼:
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]);
}
}
編輯:現在你已經從發佈的CSV文件的一些樣本數據...
好,我非常接近我的柱狀CSV數據類型假設,但確實需要更改代碼,因爲我現在知道第4個數據列包含Double數據類型值。
您需要的代碼行,指出改變:
int col4Value = Integer.parseInt(data[3]);
到處理雙數據類型值,這行代碼:
double col4Value = Double.parseDouble(data[3]);
這會幫助你一點點,而你修改代碼以滿足您的需求。
第一列和第二列必須相對於整個CSV數據文件的第一列還是第二列是唯一的? – DevilsHnd
@DevilsHnd整個CSV文件都是唯一的。因此,只有符合所有要求,我才能從CSV文件中打印出正確的行。 –
在這種情況下@Adil Khan,請查看我對您問題的回答。 – DevilsHnd