[英]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. 我的问题是 ,在寻找客户时,我必须安排while循环以仅检查每四行读取的内容。
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: 只需在循环结束时调用br.readLine()3次,并丢弃输出:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.