Java在文本中查找字符串不起作用
这是一个示例 .txt
文件:
item1
item2
myString
item3
item4
我创建了一个类来查找 .txt
文件中的字符串:
public static String lineToFind;
public static boolean lineFound;
public static void findLine() throws IOException{
try {
lineFound=true;
fstream = new FileInputStream("C:/Users/Franky/Documents/NetBeansProjects/JavaApplication5/src/Punteggi/squadre");
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));
lineToFind = "myString";
String strline;
while(br.readLine()!=null)
if(br.readLine()!=lineToFind){
lineaFound=false;
}
} catch (FileNotFoundException ex) {
Logger.getLogger(LeggiDaFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
如果 lineaFound=false,则该类在另一个类中使用;
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
findLine();
if(lineFound=true){
callFunction1();
}
if(lineFound=false){
callFunction2();
}
}
现在的问题是,即使文件中不包含“myString
”,callFunction2()
也永远不会被调用。很容易,“false
”条件永远不会发生,即使它必须发生! 谢谢
This is a sample .txt
file:
item1
item2
myString
item3
item4
I created a class to find a string in a .txt
file:
public static String lineToFind;
public static boolean lineFound;
public static void findLine() throws IOException{
try {
lineFound=true;
fstream = new FileInputStream("C:/Users/Franky/Documents/NetBeansProjects/JavaApplication5/src/Punteggi/squadre");
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));
lineToFind = "myString";
String strline;
while(br.readLine()!=null)
if(br.readLine()!=lineToFind){
lineaFound=false;
}
} catch (FileNotFoundException ex) {
Logger.getLogger(LeggiDaFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
this class is used in another class, if the lineaFound=false;
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
findLine();
if(lineFound=true){
callFunction1();
}
if(lineFound=false){
callFunction2();
}
}
Now the problem is that callFunction2()
is never called, even if "myString
" is not included in the file. Easily the "false
" condition never happens even if it has to happen!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
lineFound=false
是一项作业,而不是测试。尝试
使用or
代替
and 类似的
if (lineFound=true)
另外,请注意 在 Java 中使用带有布尔值的
==
或!=
时自动拆箱。当
b
的类型为Boolean
时,使用==
进行的测试与if (!b)
的含义有很大不同> 而不是布尔值
。lineFound=false
is an assignment, not a test.Try
or
instead of
and similarly for
if (lineFound=true)
Also, be aware of auto-unboxing in Java when using
==
or!=
with booleans.The test with
==
has a very different meaning thanif (!b)
when the type ofb
isBoolean
instead ofboolean
.这是你的问题:
你读了两行。而且如果不使用 equals 就无法对字符串进行字符串比较...
另一个答案还存在赋值问题。
This is your problem:
You are reading line twice. And you can't do string comparison on string without using equals...
There is also the assignment issue from another answer.
您需要使用
==
而不是=
。您还错误地进行了字符串比较。
if(br.readLine()!=lineToFind)
应该是if(br.readLine().equals(lineToFind))
You need to use
==
not=
.You are also doing the string comparison incorrectly.
if(br.readLine()!=lineToFind)
should beif(br.readLine().equals(lineToFind))
但是您的代码中还有几个严重的其他错误。
例如,如果文件中不是每隔一行都与该字符串匹配,则 lineFound 将输出 false。
But there are several serious other errors in your code.
For exaple, if not every second line pf the file matches the string, lineFound will come out false.
if( )
条件内要等同的任何内容都应为“==
”,而不是“=
”。对于相同上下文中的错误,应该是
这样。
另外,什么是
LineaFound
?更改
为
Anything inside a
if( )
condition to be equated is to be with '==
' and not '=
'.It should be
and so on for errors in same context.
Also, what is
LineaFound
?Change
to
尝试将
lineFound=true
替换为lineFound==true
并将
lineFound=false
替换为lineFound==false
即,使用
==
(比较运算符)而不是=
(赋值运算符)Try replacing :
lineFound=true
withlineFound==true
and
lineFound=false
withlineFound==false
That is, use
==
( comparison operator ) instead of=
( assignment operator )尽管已经提到了
==
而不是=
,并且使用equals
来比较字符串,并且只比较每隔一行,但仍存在两个问题:1 - 如果文件的最后一行不是
“myString”
,findLine
中的循环会导致lineaFound = false
>,无论其他行是否是“myString”(如果使用equals
也是如此)。2 - 为什么使用
DataInputStream
?仅当读取DataOutputStream
创建的数据时才需要这样做。Two problems, despite the already mentioned
==
instead of=
, and the use ofequals
to compare Strings, and only comparing every second line:1 - The loop in
findLine
results inlineaFound = false
if the last line of the file is not"myString"
, no matter if some other line is "myString" (also if usingequals
).2 - Why are you using a
DataInputStream
? This is only needed to read data created by aDataOutputStream
.