modify any text file which has lines; with array list in java and groovy
java code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String inputData = "Z99@MDNET0145J8391750181750300 XQ000310 00145\n" +
"EOF@MDNET0145 XQ000310000001\n" +
"Z99@MDNET0145J8391750137576400 XQ000310 00145\n" +
"C1120712641 375764002023062983917501 000000000 0000000014987439072388 0000600000004650000000001\n" +
"C1113062901471817503132023062983917501 0000000014987439076607 0000120000000000000000002\n" +
"C1113062901471817503132023062983917501 0000000014987439076607 0000120000000000000000002\n" +
"EOF@MDNET0145 XQ000310000001";
BufferedReader reader = new BufferedReader(new StringReader(inputData));
String line;
List<String> cLines = new ArrayList<>();
try {
while ((line = reader.readLine()) != null) {
line = line.trim();
cLines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < cLines.size(); i++) {
String cLine = cLines.get(i);
// Check if the current line starts with 'C' and the previous line starts with 'Z'
if (cLine.startsWith("C") && i > 0 && cLines.get(i - 1).startsWith("Z")) {
System.out.println(cLines.get(i - 1));
System.out.println(cLine);
// Check if the current line starts with 'C' and the next line starts with 'E'
if (i < cLines.size() - 1 && cLines.get(i + 1).startsWith("E")) {
System.out.println(cLines.get(i + 1));
}
}
// Check if the current line starts with 'C' and the next line starts with 'E'
else if (cLine.startsWith("C") && i < cLines.size() - 1 && cLines.get(i + 1).startsWith("E")) {
System.out.println(cLine);
System.out.println(cLines.get(i + 1));
}
// Display all C lines
else if (cLine.startsWith("C")) {
System.out.println(cLine);
System.out.println("");
}
}
}
}

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home