简体   繁体   English

在while循环的谓词中分配值-BufferedReader

[英]Assigning values in the predicate of a while loop - BufferedReader

I have a piece of code that looks like this: 我有一段代码看起来像这样:

public void cinemaFromFile(String fileName) throws IOException {

    BufferedReader reader = new BufferedReader(new FileReader(fileName));
    String line;
    while ((line = reader.readLine()) != null) {

        if (line.equals("Movies")) {

            while ((line = reader.readLine()) != null 
                    && !line.equals("Theaters")) {

                String currentline = line; 
                String[] parts = currentline.split(":");
                String part1 = parts[0]; 
                String part2 = parts[1]; 
                movies.add(new Movie(part1, part2));
            }
        }

    reader.close();
}

As you can see, I assign line a value in the termination clause of the while loop. 如您所见,我在while循环的终止子句中为行分配了一个值。

I am new to BufferedReaders and I am looking for an alternative to this. 我是BufferedReaders的新手,我正在寻找替代方法。 IE, I want to have the assignment statement("line = reader.readLine()") in a separate statement, external to the while loop termination clause. IE,我想在while循环终止子句的外部有一条单独的语句中的赋值语句(“ line = reader.readLine()”)。

I have tried moving "line = reader.readLine()" into the body of the while loop but that doesn't work. 我尝试将“ line = reader.readLine()”移到while循环的主体中,但这不起作用。 I have also tried putting it right before the while loop, which also doesn't work. 我也尝试过将它放在while循环之前,这也不起作用。

I think I have fundamentally misunderstood how BufferedReaders work, can they only iterate through the lines in while loops? 我认为我从根本上误解了BufferedReaders的工作方式,它们只能在while循环中的各行之间进行迭代吗?

To pull the assignment out of the while loop condition, you must understand that the while loop condition is evaluated once before the first iteration, and after the end of each iteration. 要使分配退出while循环条件,您必须了解while循环条件在第一次迭代之前和每次迭代结束之后都会被评估一次。

You can emulate that by placing the assignment before the while loop even begins, and also adding it to the end of the while loop body. 您可以通过将赋值放置在while循环甚至开始之前并将其添加到while循环主体的末尾来进行模拟。

line = reader.readLine();
while (line != null) {
    // Rest of body is untouched

    // Read again at the end.
    line = reader.readLine();
}

This will operate the same. 此操作将相同。 It may even be a little clearer than having the assignment in the condition of the while loop, even if it's a little less concise. 它甚至比在while循环条件下进行赋值更清晰一些,即使它不够简洁。

The BufferedReader does nothing special in this regard with while loops. 对于while循环, BufferedReader在这方面没有什么特别的。 It just returns null when readLine() is called and there's no input left. readLine()被调用并且没有剩余输入时,它只是返回null

BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while (true) {
    line = reader.readLine();
    if(line == null)
        break;

    if (line.equals("Movies")) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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