Java在文本中查找字符串不起作用

发布于 2024-10-27 13:39:38 字数 1327 浏览 6 评论 0原文

这是一个示例 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

如日中天 2024-11-03 13:39:38

lineFound=false 是一项作业,而不是测试。

尝试

if (!lineFound)

使用or

if (lineFound == false)

代替

if (lineFound=false)

and 类似的 if (lineFound=true)

另外,请注意 在 Java 中使用带有布尔值的 ==!= 时自动拆箱

Boolean b = possiblyNullOrABoolean();
if (b == false) {
  ...
}

b 的类型为 Boolean 时,使用 == 进行的测试与 if (!b) 的含义有很大不同> 而不是布尔值

lineFound=false is an assignment, not a test.

Try

if (!lineFound)

or

if (lineFound == false)

instead of

if (lineFound=false)

and similarly for if (lineFound=true)

Also, be aware of auto-unboxing in Java when using == or != with booleans.

Boolean b = possiblyNullOrABoolean();
if (b == false) {
  ...
}

The test with == has a very different meaning than if (!b) when the type of b is Boolean instead of boolean.

獨角戲 2024-11-03 13:39:38

这是你的问题:

String strline;
 while(br.readLine()!=null)
    if(br.readLine()!=lineToFind){
    lineaFound=false;
  }

你读了两行。而且如果不使用 equals 就无法对字符串进行字符串比较...

String strline;
while(strline = br.readLine()!=null)
    if(strline.equals(lineToFind)){
    lineaFound=false;
}

另一个答案还存在赋值问题。

This is your problem:

String strline;
 while(br.readLine()!=null)
    if(br.readLine()!=lineToFind){
    lineaFound=false;
  }

You are reading line twice. And you can't do string comparison on string without using equals...

String strline;
while(strline = br.readLine()!=null)
    if(strline.equals(lineToFind)){
    lineaFound=false;
}

There is also the assignment issue from another answer.

梦情居士 2024-11-03 13:39:38

您需要使用 == 而不是 =

 if(lineFound==true){
    callFunction1();
    }
    if(lineFound==false){
    callFunction2();
    }

您还错误地进行了字符串比较。

if(br.readLine()!=lineToFind) 应该是 if(br.readLine().equals(lineToFind))

You need to use == not =.

 if(lineFound==true){
    callFunction1();
    }
    if(lineFound==false){
    callFunction2();
    }

You are also doing the string comparison incorrectly.

if(br.readLine()!=lineToFind) should be if(br.readLine().equals(lineToFind))

帝王念 2024-11-03 13:39:38

但是您的代码中还有几个严重的其他错误。
例如,如果文件中不是每隔一行都与该字符串匹配,则 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.

帅哥哥的热头脑 2024-11-03 13:39:38

if( ) 条件内要等同的任何内容都应为“==”,而不是“=”。

'=' is an assignment operator.  
'==' checks for the equivalence. 

对于相同上下文中的错误,应该是

if (lineFound == false)   

这样。

另外,什么是 LineaFound

更改

LineaFound = false;

LineFound = false;

Anything inside a if( ) condition to be equated is to be with ' == ' and not '='.

'=' is an assignment operator.  
'==' checks for the equivalence. 

It should be

if (lineFound == false)   

and so on for errors in same context.

Also, what is LineaFound ?

Change

LineaFound = false;

to

LineFound = false;
溺深海 2024-11-03 13:39:38

尝试将

lineFound=true 替换为

lineFound==true

并将 lineFound=false 替换为

lineFound==false

即,使用 == (比较运算符)而不是 = (赋值运算符)

Try replacing :

lineFound=true with

lineFound==true

and lineFound=false with

lineFound==false

That is, use == ( comparison operator ) instead of = ( assignment operator )

倾听心声的旋律 2024-11-03 13:39:38

尽管已经提到了 == 而不是 =,并且使用 equals 来比较字符串,并且只比较每隔一行,但仍存在两个问题:

1 - 如果文件的最后一行不是“myString”findLine中的循环会导致lineaFound = false >,无论其他行是否是“myString”(如果使用 equals 也是如此)。

    ...
    lineaFound = false;
    String strline;
    while ((strLine = br.readLine()) != null) {
        if (strLine.equals(lineToFind)) {
            lineaFound = true;
            break;  // we are done, exit the loop
        }
    }
    ...

2 - 为什么使用DataInputStream?仅当读取 DataOutputStream 创建的数据时才需要这样做。

Two problems, despite the already mentioned == instead of =, and the use of equals to compare Strings, and only comparing every second line:

1 - The loop in findLine results in lineaFound = false if the last line of the file is not "myString", no matter if some other line is "myString" (also if using equals).

    ...
    lineaFound = false;
    String strline;
    while ((strLine = br.readLine()) != null) {
        if (strLine.equals(lineToFind)) {
            lineaFound = true;
            break;  // we are done, exit the loop
        }
    }
    ...

2 - Why are you using a DataInputStream? This is only needed to read data created by a DataOutputStream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文