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
0000, and you must always also output0001and0002, then why do you need this complex code when you can just output sequences of0000,0001,0002times number of lines in file?