如何在Java中将字符串列表连接起来

huangapple go评论72阅读模式
英文:

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 -->

&lt;table style=&quot;border-collapse:collapse;border-spacing:0&quot; class=&quot;tg&quot;&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&quot;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&quot;&gt;&lt;span style=&quot;font-weight:bold&quot;&gt;record00&lt;/span&gt;&lt;/th&gt;&lt;th style=&quot;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&quot;&gt;&lt;span style=&quot;font-weight:bold&quot;&gt;record01&lt;/span&gt;&lt;/th&gt;&lt;th style=&quot;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&quot;&gt;&lt;span style=&quot;font-weight:bold&quot;&gt;record02&lt;/span&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;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&quot;&gt;0000&lt;/td&gt;&lt;td style=&quot;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&quot;&gt;0001&lt;/td&gt;&lt;td style=&quot;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&quot;&gt;0002&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;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&quot;&gt;0000&lt;/td&gt;&lt;td style=&quot;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&quot;&gt;&lt;/td&gt;&lt;td style=&quot;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&quot;&gt;0002&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;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&quot;&gt;0000&lt;/td&gt;&lt;td style=&quot;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&quot;&gt;0001&lt;/td&gt;&lt;td style=&quot;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&quot;&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;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&quot;&gt;0000&lt;/td&gt;&lt;td style=&quot;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&quot;&gt;0001&lt;/td&gt;&lt;td style=&quot;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&quot;&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;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&quot;&gt;0000&lt;/td&gt;&lt;td style=&quot;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&quot;&gt;&lt;/td&gt;&lt;td style=&quot;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&quot;&gt;0002&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;

<!-- end snippet -->

Current java code I created:

    List&lt;String&gt; collectfilelines = new ArrayList&lt;&gt;();
try (BufferedReader reader = new BufferedReader(new FileReader(&quot;C:\\merged\\Merged.txt&quot;))) {
String line = reader.readLine();
while (line != null) {
collectfilelines.add(line);
line = reader.readLine();
}
} catch (IOException exc) {
System.out.println(&quot;problem reading the  file&quot; + exc);
}
String records = &quot;&quot;;
int i;
int imax = collectfilelines.size();
for (i = 0; i &lt; imax; i++) {
String line = collectfilelines.get(i);
String currentline;
String linenext;
String previousline = null;
if (i &gt;0) {
previousline = collectfilelines.get(i - 1).substring(0,4);
} else {
previousline = collectfilelines.get(i).substring(0,4);
}
if (i &lt; imax-1) {
linenext = collectfilelines.get(i + 1).substring(0,4);
} else {
linenext = &quot;9999&quot;;
}
currentline = line.substring(0,4);
if (currentline.equals(&quot;0000&quot;)) {
records = line;
}
if (currentline.equals(&quot;0001&quot;) &amp;&amp; (linenext.equals(&quot;0002&quot;))) {
records = records + &quot; &quot; + line;
}
if (currentline.equals(&quot;0001&quot;) &amp;&amp; ((linenext.equals(&quot;0000&quot;))||(linenext.equals(&quot;9999&quot;)))) {
records = records + &quot; &quot; + line;
System.out.println(records);}
if (currentline.equals(&quot;0002&quot;)) {
if (previousline.equals(&quot;0000&quot;)) {
records = records + &quot;            &quot; + line;
}
if (previousline.equals(&quot;0001&quot;)) {
records = records + &quot; &quot; + 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&lt;String&gt; collectfilelines) {
StringBuilder record = new StringBuilder();
int prevFieldNo = 0;
for (String line : collectfilelines) {
int fieldNo = Integer.parseInt(line.substring(0, 4));
if (fieldNo &lt;= prevFieldNo) {
if (record.length() != 0)
System.out.println(record);
record.setLength(0);
prevFieldNo = 0;
}
for (; prevFieldNo &lt; fieldNo; prevFieldNo++)
record.append(&#39;,&#39;);
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(&quot;0000&quot;,&quot;0001&quot;,&quot;0002&quot;,&quot;0000&quot;,&quot;0002&quot;,&quot;0000&quot;,&quot;0001&quot;,&quot;0000&quot;,&quot;0001&quot;));
printCsv(Arrays.asList(&quot;0000 The&quot;, &quot;0001 quick&quot;, &quot;0002 brown&quot;,
&quot;0000 fox&quot;, &quot;0002 jumps&quot;, &quot;0005 over&quot;, &quot;0009 the&quot;,
&quot;0000 lazy&quot;, &quot;0001 dog&quot;));

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

huangapple
  • 本文由 发表于 2020年9月3日 17:33:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63720792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定