0

I am trying to reform/reconstruct the string values from list. I am reading values from a text file. values in text file like below format.

    H|013450107776|10/15/2019
    D|TXN001|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR SERVICE|Successful
    D|TXN002|10/15/2019|013458001581|LCUATADA04|1500.00|INVOICE 001|Successful
    D|TXN003|10/15/2019|013450107784|LCUATADA01|1750.00|SEPT PAYMENT|Successful
    H|007442500211|11/05/2019
    D|1000000489|007442500211|0009204332|85585.44|SEPT PAYMENT|Successful
    H|007442500213|11/05/2019
    D|1000000489|007442500211|0009204332|85585.44|SEPT PAYMENT|Successful
    D|1000000490|007442500211|0009204332|85585.44|SEPT PAYMENT|Successful
    D|1000000491|007442500211|0009204332|85585.44|SEPT PAYMENT|Successful

find below code for reading file.

 public Integer  readFile(String fileName,String path){  
        List<String> lineList = new ArrayList<>();
        try {
            reader = new BufferedReader(new FileReader(
                    path+"/"+fileName));
            String line = reader.readLine();
            count =0;
            while (line != null) {
                lineList.add(line);
                count ++;
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
        }
        String packet=searchHeader(lineList);
        try{
            unmarshallResponsePacket(packet); 
        }catch(Exception e){
        }
        return count;
    }

lineList contains values like

[ H|013450107776|10/15/2019,D|TXN001|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR SERVICE|Successful,D|TXN002|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR SERVICE|Successful,D|TXN002|10/15/2019|013458001581|LCUATADA04|1500.00|INVOICE 001|Successful, D|TXN003|10/15/2019|013450107784|LCUATADA01|1750.00|SEPT PAYMENT|Successful,H|007442500211|11/05/2019,D|1000000489|007442500211|0009204332|85585.44|SEPTPAYMENT|Successful...] 


  How i can form the string value(header information followed by detail 
  information) like
 "H|013450207776|10/15/2019
  D|0000TXN001|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR 
  SERVICE|Successful" from the List

1 Answer 1

1
using below code i resolved the issue.

private static String packetList(List<String> oldPacketList) {
        StringBuffer sb3 = new StringBuffer();

        String header="" ;  

        for(int i = 0; i < oldPacketList.size(); i++) {
            if(oldPacketList.get(i).startsWith("H")) {
                headertest=oldPacketList.get(i);
                continue;
            }
            else {
                sb3.append(header.trim().toString()+(System.getProperty("line.separator")));
                sb3.append(oldPacketList.get(i).trim().toString()+(System.getProperty("line.separator")));


            }
        }

        System.out.println("----------------output--------------------");

        return sb3.toString().trim();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.