简体   繁体   中英

how to Concatenate Strings from a list of strings Java

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM