如何解决如何从字符串列表Java连接字符串
我无法将以下格式的字符串列表连接到csv。
当前列表(从文件中读取的字符串列表中)
- 0000
- 0001
- 0002
- 0000
- 0002
- 0000
- 依此类推...
记录始终为0000,其他记录为可选。每行都是一条记录(实际上是交易记录)。如果缺少0001/0002,我需要填充空间
我所做的是一个复杂的代码,还需要检查上一行和下一行。例如,如果当前行是0002,下一行是0000,则打印连接的字符串。应该有一个更简单的方法和更简单的逻辑。
所需的打印/ CSV文件输出显示在下面的html表中,您必须单击运行代码片段
<table style="border-collapse:collapse;border-spacing:0" class="tg"><thead><tr><th style="background-color:#efefef;border-color:#000000;border-style:solid;border-width:1px;font-family:Arial,sans-serif;font-size:14px;font-weight:normal;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal"><span style="font-weight:bold">record00</span></th><th style="background-color:#efefef;border-color:#000000;border-style:solid;border-width:1px;font-family:Arial,sans-serif;font-size:14px;font-weight:normal;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal"><span style="font-weight:bold">record01</span></th><th style="background-color:#efefef;border-color:#000000;border-style:solid;border-width:1px;font-family:Arial,sans-serif;font-size:14px;font-weight:normal;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal"><span style="font-weight:bold">record02</span></th></tr></thead><tbody><tr><td style="background-color:#efefef;border-color:#000000;border-style:solid;border-width:1px;font-family:Arial,sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">0000</td><td style="background-color:#efefef;border-color:#000000;border-style:solid;border-width:1px;font-family:Arial,sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">0001</td><td style="background-color:#efefef;border-color:#000000;border-style:solid;border-width:1px;font-family:Arial,sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">0002</td></tr><tr><td style="background-color:#efefef;border-color:#000000;border-style:solid;border-width:1px;font-family:Arial,sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal"></td><td style="background-color:#efefef;border-color:#000000;border-style:solid;border-width:1px;font-family:Arial,sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal"></td></tr><tr><td style="background-color:#efefef;border-color:#000000;border-style:solid;border-width:1px;font-family:Arial,sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">0002</td></tr></tbody></table>
我创建的当前Java代码:
List<String> collectfilelines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("C:\\merged\\Merged.txt"))) {
String line = reader.readLine();
while (line != null) {
collectfilelines.add(line);
line = reader.readLine();
}
} catch (IOException exc) {
System.out.println("problem reading the file" + exc);
}
String records = "";
int i;
int imax = collectfilelines.size();
for (i = 0; i < imax; i++) {
String line = collectfilelines.get(i);
String currentline;
String linenext;
String previousline = null;
if (i >0) {
previousline = collectfilelines.get(i - 1).substring(0,4);
} else {
previousline = collectfilelines.get(i).substring(0,4);
}
if (i < imax-1) {
linenext = collectfilelines.get(i + 1).substring(0,4);
} else {
linenext = "9999";
}
currentline = line.substring(0,4);
if (currentline.equals("0000")) {
records = line;
}
if (currentline.equals("0001") && (linenext.equals("0002"))) {
records = records + " " + line;
}
if (currentline.equals("0001") && ((linenext.equals("0000"))||(linenext.equals("9999")))) {
records = records + " " + line;
System.out.println(records);}
if (currentline.equals("0002")) {
if (previousline.equals("0000")) {
records = records + " " + line;
}
if (previousline.equals("0001")) {
records = records + " " + line;
}
System.out.println(records);
}
}
}
**编辑:**出于某些原因,我在https://coderanch.com/t/734045/java/Complex-array-operation-java-checking#3414904
上发布了类似/几乎相同的问题解决方法
您可以这样做。
static void printCsv(List<String> collectfilelines) {
StringBuilder record = new StringBuilder();
int prevFieldNo = 0;
for (String line : collectfilelines) {
int fieldNo = Integer.parseInt(line.substring(0,4));
if (fieldNo <= prevFieldNo) {
if (record.length() != 0)
System.out.println(record);
record.setLength(0);
prevFieldNo = 0;
}
for (; prevFieldNo < fieldNo; prevFieldNo++)
record.append(',');
record.append(line);
}
if (record.length() != 0)
System.out.println(record);
}
但是,我强烈建议在生成CSV格式的输出时使用CSV库,以确保数据中包含逗号时的正确编码。
测试
printCsv(Arrays.asList("0000","0001","0002","0000","0001"));
printCsv(Arrays.asList("0000 The","0001 quick","0002 brown","0000 fox","0002 jumps","0005 over","0009 the","0000 lazy","0001 dog"));
输出
0000,0001,0002
0000,0001
0000,0001
0000 The,0001 quick,0002 brown
0000 fox,0002 jumps,0005 over,0009 the
0000 lazy,0001 dog
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。