你的问题中的要求是相当混乱,事情不要”如果没有示例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