我如何阅读此条件并计算我的值?

发布于 2024-08-21 18:11:44 字数 1131 浏览 5 评论 0原文

我有一个动态大文件,我想检查一个布尔值,然后如果它是真的,请计数一个东西,

请帮助我

,例如这是我的文件(它可能有 20000 行)

.
.
.
fsfds fdsfsdf gfhgfh value1=true fdsfd fdfdsr ewref

dfdfsd dsfds  dfdf fdsfsd 
dfdfsd dsfds myval dfdf fdsfsd 
dfdfsd dsfds dfdf fdsfsd 
dfdfsd dsfds myval dfdf fdsfsd 

fdfdfddsfds
fdfdsfdfdsfdfdsfsdfdsfdsfdsfds
.
.
.

我编写了这段代码来处理它,但我可以不是因为在这种情况下,我逐行阅读,我必须检查 value1 是否为真,然后在 2 行中,我必须计算 myval ...

public void countMethod() {
        int i = 0; //count
        try {
            BufferedReader input =
                    new BufferedReader(new FileReader(new File("I:\\1.txt")));
            String line;
            while ((line = input.readLine()) != null) {
                if (line.substring(line.indexOf("value1")).split("=")[1].equals("true")) {
                    if (line.indexOf("myval") != -1)
                        i++;
                }
            }
            input.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Count is : " + i);
    }

我如何检查条件,然后如果为真,我计算 myvals ?

多谢

I have a dynamic big File and I want check a Boolean value and then if it's true, get count of a thing

please help me

for example this is my file (it maybe has 20000 lines)

.
.
.
fsfds fdsfsdf gfhgfh value1=true fdsfd fdfdsr ewref

dfdfsd dsfds  dfdf fdsfsd 
dfdfsd dsfds myval dfdf fdsfsd 
dfdfsd dsfds dfdf fdsfsd 
dfdfsd dsfds myval dfdf fdsfsd 

fdfdfddsfds
fdfdsfdfdsfdfdsfsdfdsfdsfdsfds
.
.
.

I wrote this code to handle it but I can't because in this case I read Line by Line and I must check if value1 is true then in 2 line after I must count myval ...

public void countMethod() {
        int i = 0; //count
        try {
            BufferedReader input =
                    new BufferedReader(new FileReader(new File("I:\\1.txt")));
            String line;
            while ((line = input.readLine()) != null) {
                if (line.substring(line.indexOf("value1")).split("=")[1].equals("true")) {
                    if (line.indexOf("myval") != -1)
                        i++;
                }
            }
            input.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Count is : " + i);
    }

How can I check condition and after that if it's true I count myvals?

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉城メ夜风 2024-08-28 18:11:44

如果您只想遍历该文件,只需检查每一行并检查其 myval 计数以及是否有 value1 = true。您将知道 myval 的计数是多少,但除非 value1 为 true,否则您不必报告它。

If you only want to make one pass through the file, just inspect each line and check both its count of myval and whether it has value1 = true. You'll know what the count of myval is, but you won't have to report it unless value1 is true.

泪眸﹌ 2024-08-28 18:11:44

尝试:

boolean found = false;
while ((line = input.readLine()) != null) {
  if (!found)
    found=line.substring(line.indexOf("value1")).split("=")[1].equals("true");
  if (found && line.indexOf("myval") != -1)
    i++;
}

Try:

boolean found = false;
while ((line = input.readLine()) != null) {
  if (!found)
    found=line.substring(line.indexOf("value1")).split("=")[1].equals("true");
  if (found && line.indexOf("myval") != -1)
    i++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文