views:

22

answers:

2

Hello everyone

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

A: 

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++;
}
nicerobot
The `myval` doesn't necessarily appear on the same line as `value1=true`, at least based on his example.
John Feminella
I can't because in output i can check just this line "fsfds fdsfsdf gfhgfh value1=true fdsfd fdfdsr ewref" and how can i continue while loop???
Mike Redford
The code above will loop until EOF. The first time it finds a line with "value1" followed by "=true", it'll begin counting occurrences of "myval" on that line and all that follow. It'll continue doing that until EOF.
nicerobot
+1  A: 

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.

John Feminella