英文:
.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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论