gpt4 book ai didi

Java - 读取文本文件

转载 作者:行者123 更新时间:2023-11-29 02:58:33 25 4
gpt4 key购买 nike

我有一个文本文件如下:

Past Dues / Refunds / Subsidy
Arrears / Refunds
Amount
2013.23
Period to which
it relates
Since OCT-15

现在,我如何提取下一行“Amount”中的数据。我已经尝试使用 boolean 值,检查上面和下面的行。

有没有其他方法可以做到。

我的代码:

boolean isGroup=false;
while(line = br.readline() != null){
if(line.equals("Amount"){
isGroup=true;
}
if(line.equals("Period to which") && isGroup)
isGroup=false;
if(isGroup){
//read line and check whether it is null or not
String amount = line;
}
}

请帮助。谢谢

最佳答案

您的方法非常好。您通过设置 boolean 值,然后在循环的同一迭代中使用而犯了一个小错误。

如果您执行以下操作,您应该没问题:

String amount = "No amount found";
boolean isGroup=false;
while(line = br.readline() != null) {
// Check all your conditions to see if this is the line you care about
if(isGroup){
amount = line;
isGroup = false; // so you only capture this once
continue;
}
else if (isOtherCondition) {
// handle other condition;
isOtherCondition = false; // so you only capture this once
continue;
}

// Check the contents of lines to see if it's one you want to read next iteration
if(line.equals("Amount"){
isGroup=true;
}
else if (line.equals("Some Other Condition")) {
isOtherCondition = true;
}
}

这就是您所需要的。 break; 只是为了让你不必担心抢到金额后会发生什么。

关于Java - 读取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36668915/

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