gpt4 book ai didi

java - 从数组列表中返回字符串值

转载 作者:行者123 更新时间:2023-12-01 21:24:24 24 4
gpt4 key购买 nike

我正在尝试改革/重建列表中的字符串值。我正在从文本文件中读取值。文本文件中的值如下格式。

    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

找到以下用于读取文件的代码。

 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 包含类似

的值
[ 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

最佳答案

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();
}

关于java - 从数组列表中返回字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58835846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com