英文:
how to Concatenate Strings from a list of strings 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);
}
}
Note: The provided code is a direct translation of the Java code you posted, but without any changes or improvements.
英文:
I am unable to concatenate a list of strings from the following format to a csv.
Current list (from a list of string read from the file)
- 0000
- 0001
- 0002
- 0000
- 0002
- 0000
- and so on...
Records always have 0000, other records are optional. Each line is a record (transaction record actually). if 0001/0002 is missing I need to fill space
What I have done is a complex code to check previous line and next line as well. Example if current line is 0002 and next line is 0000 then print the concatenated string. There should be a simpler way, and easier logic for this.
Desired output of the print /CSV file is Represented in the below html table YOU HAVE TO CLICK RUN CODE SNIPPET
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-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">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"></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">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"></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">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"></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">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"></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></tbody></table>
<!-- end snippet -->
Current java code I created:
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);
}
}
}
**Edit:**For certain reasons I posted similar/almost identical issue on https://coderanch.com/t/734045/java/Complex-array-operation-java-checking#3414904
答案1
得分: 1
以下是翻译好的部分:
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","0002","0000","0001","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,,0002
0000,0001
0000,0001
0000 The,0001 quick,0002 brown
0000 fox,,0002 jumps,,,0005 over,,,,0009 the
0000 lazy,0001 dog
英文:
You can do it like this.
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);
}
However, I would highly recommend using a CSV Library when generating CSV formatted output, to ensure correct encoding when the data contains commas.
Tests
printCsv(Arrays.asList("0000","0001","0002","0000","0002","0000","0001","0000","0001"));
printCsv(Arrays.asList("0000 The", "0001 quick", "0002 brown",
"0000 fox", "0002 jumps", "0005 over", "0009 the",
"0000 lazy", "0001 dog"));
Outputs
0000,0001,0002
0000,,0002
0000,0001
0000,0001
0000 The,0001 quick,0002 brown
0000 fox,,0002 jumps,,,0005 over,,,,0009 the
0000 lazy,0001 dog
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论