.contains()在BufferedReader中无法正常工作。

huangapple go评论75阅读模式
英文:

.contains() not working with BufferedReader

问题

以下是翻译好的内容:

我正在尝试创建一个程序,该程序会检查一个文件,并在其包含单词“TRUE”的行时将这些行打印回给我。

以下是文件的内容:

TRUE,TRUE
FALSE,TRUE
FALSE,FALSE
TRUE,FALSE
TRUE,TRUE
TRUE,FALSE

以下是程序的代码:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    BufferedReader reader;
    try {
        reader = new BufferedReader(new FileReader(
                "C:\\Users\\tree3\\Desktop\\Programming\\file.txt"));
        String line = reader.readLine();
        while (line != null) {

            if(line.contains("TRUE")) {    
                System.out.println(line);
                // 读取下一行
                line = reader.readLine();
            } else {
                System.out.println("false");
                // 读取下一行
                line = reader.readLine();
            }


        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

该程序只会无限制地打印 false

这是为什么呢?

英文:

i am trying to create a program that checks a file and prints lines back to me if they contain the word "TRUE"

here is the file contents

TRUE,TRUE
FALSE,TRUE
FALSE,FALSE
TRUE,FALSE
TRUE,TRUE
TRUE,FALSE

here is the program

public static void main(String[] args) {
	// TODO Auto-generated method stub
	
	BufferedReader reader;
	try {
		reader = new BufferedReader(new FileReader(
				"C:\\Users\\tree3\\Desktop\\Programming\\file.txt"));
		String line = reader.readLine();
		while (line != null) {
			
			if(line.contains("TRUE")) {	
				System.out.println(line);
				// read next line
				line = reader.readLine();
			} else {
				System.out.println("false");
			}
			
			
		}
		reader.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
	
}

the program just prints false indefinitely

why is this?

答案1

得分: 0

public static void main(String[] args) {
    try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\tree3\\Desktop\\Programming\\file.txt"))) {
        String line = reader.readLine();
        while (line != null) {
            if (line.contains("TRUE")) {
                System.out.println(line);
            } else {
                System.out.println("false");
            }
            // read next line
            line = reader.readLine();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Do not read next line in side an if statement. What if the if statement is not true? You will never go ahead and be stuck on that line indefinitely.

Use try-with-resources (Java 7+) and don't worry about closing resources.


----------

Cleaner (Java 8+):

try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\tree3\\Desktop\\Programming\\file.txt"))) {
    reader.lines().forEach(line -> System.out.println(line.contains("TRUE")));
} catch (IOException e) {
    e.printStackTrace();
}
英文:
public static void main(String[] args) {
    try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\tree3\\Desktop\\Programming\\file.txt"))) {
        String line = reader.readLine();
        while (line != null) {
            if (line.contains("TRUE")) {
                System.out.println(line);
            } else {
                System.out.println("false");
            }
            // read next line
            line = reader.readLine();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Do not read next line in side an if statement. What if the if statement is not true? You will never go ahead and be stuck on that line indefinitely.

Use try-with-resources (Java 7+) and don't worry about closing resources.


Cleaner (Java 8+):

try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\tree3\\Desktop\\Programming\\file.txt"))) {
    reader.lines().forEach(line -> System.out.println(line.contains("TRUE")));
} catch (IOException e) {
    e.printStackTrace();
}

huangapple
  • 本文由 发表于 2020年8月18日 06:02:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63459215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定