0

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

asked Sep 3, 2020 at 9:33
6
  • 2
    I don't understand this. You say "from csv", but you show HTML as an example. You show HTML as example, but your code works lines from a text file and uses only first couple of characters (from HTML? that'd be mostly whitespaces then). Also, what do the "records" have to do with any of this? Commented Sep 3, 2020 at 9:38
  • @M.Prokhorov You have to click the "Run code snippet" to see what OP meant to show you. Commented Sep 3, 2020 at 9:44
  • @Andreas, I don't have to run a snippet to understand the simple HTML table. I don't understand what any of this HTML has to do with CSV. Or if that's HTML that supposed to represent contents of CSV, then why OP chose more complex format of data transfer to represent the less complex format of data transfer? Commented Sep 3, 2020 at 9:49
  • Each line is a record (transaction record actually). There is always 000, but if 0001 is missing I need to fill space. @M. Prokhorov The HTML table is to represnent how the csv will be generated. Commented Sep 3, 2020 at 9:49
  • 1
    If there's always 0000, and you must always also output 0001 and 0002, then why do you need this complex code when you can just output sequences of 0000,0001,0002 times number of lines in file? Commented Sep 3, 2020 at 9:51

1 Answer 1

1

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
answered Sep 3, 2020 at 10:04
1
  • very elegant solution. I haven't tested it but it looks great. I didn't thought this way. Thank you! Commented Sep 3, 2020 at 10:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.