【发布时间】:2020-11-08 23:40:49
【问题描述】:
我有一个格式如下的 .csv 文件:
ID,date,itemName
456,1-4-2020,Lemon
345,1-3-2020,Bacon
345,1-4-2020,Sausage
123,1-1-2020,Apple
123,1-2-2020,Pineapple
234,1-2-2020,Beer
345,1-4-2020,Cheese
我已经实现了遍历文件的算法,扫描第一个数字并按降序排序并生成新的输出:
123,1-1-2020,Apple
123,1-2-2020,Pineapple
234,1-2-2020,Beer
345,1-3-2020,Bacon
345,1-4-2020,Cheese
345,1-4-2020,Sausage
456,1-4-2020,Lemon
我的问题是,我如何实现我的算法来生成计算重复的第一个数字条目的输出并重新格式化它以使其看起来像这样......
123,1-1-2020,1,Apple
123,1-2-2020,1,Pineapple
234,1-2-2020,1,Beer
345,1-3-2020,1,Bacon
345,1-4-2020,2,Cheese,Sausage
456,1-4-2020,1,Lemon
...以便计算每个 ID 的出现次数,用次数表示,如果该 ID 的日期也相同,则将项目名称合并到同一行。下面是我的源代码(.csv 中的每一行都被制成一个名为“receipt”的对象,该对象具有 ID、日期和名称以及它们各自的 get() 方法):
public class ReadFile {
private static List<Receipt> readFile() {
List<Receipt> receipts = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader("dataset.csv"));
// Move past the first title line
reader.readLine();
String line = reader.readLine();
// Start reading from second line till EOF, split each string at ","
while (line != null) {
String[] attributes = line.split(",");
Receipt attribute = getAttributes(attributes);
receipts.add(attribute);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return receipts;
}
private static Receipt getAttributes(String[] attributes) {
// Get ID located before the first ","
long memberNumber = Long.parseLong(attributes[0]);
// Get date located after the first ","
String date = attributes[1];
// Get name located after the second ","
String name = attributes[2];
return new Receipt(memberNumber, date, name);
}
// Parse the data into new file after sorting
private static void parse(List<Receipt> receipts) {
PrintWriter output = null;
try {
output = new PrintWriter("output.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// For each receipts, assert the text output stream is not null, print line.
for (Receipt p : receipts) {
assert output != null;
output.println(p.getMemberNumber() + "," + p.getDate() + "," + p.getName());
}
assert output != null;
output.close();
}
// Main method, accept input file, sort and parse
public static void main(String[] args) {
List<Receipt> receipts = readFile();
QuickSort q = new QuickSort();
q.quickSort(receipts);
parse(receipts);
}
}
【问题讨论】:
-
我不明白你的输出。您在输出的两条单独的行上有 345。我认为第一个数字是用于组合线条的数字。
-
永远不要将日期存储为 MM-DD-YYYY 或 DD-MM-YYYY。始终符合 ISO 8601 (YYYY-MM-DD)。
-
@jarmod 这可能是 OP 无法控制的要求。
-
@Mint 那你为什么在第一行结合苹果和菠萝。两个不同的日期。
-
@WJS 是的,但是每个程序员越早学习日期表示的基础知识越好。