简体   繁体   中英

Make FileReader read every fourth line with a loop

My problems is that I have to arrange, when searching for a customer, arrange the while loop to only examine every fourth line read.

This is the code I already have on this problem:

BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;

while ((line = br.readLine()) != null)
{
    ...
}

br.close();

Does anybody know what needs to be at the place of "..."?

Thanks!

Something along the lines of

int i = 0;
while ((line = br.readLine()) != null)
{
   i++;
   if (i % 4 == 0)
   {
      // if i is divisible by 4, then
      // your actual code will get executed
      ...
   }

}

Just call br.readLine() 3 times at the end of the loop, discarding the output:

BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;

while ((line = br.readLine()) != null)
{
    ...
    for(int i=0;i<3;i++){ br.readLine(); }
}

br.close();
BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;
int count 0;
while ((line = br.readLine()) != null)
{
    if (count!=3)
        count++;
    else {
        // Do something?
        count=0;
    }

}

br.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM