gpt4 book ai didi

java - 错误: cannot find symbol - Internal Search Engine

转载 作者:行者123 更新时间:2023-12-02 05:42:18 25 4
gpt4 key购买 nike

我需要创建一个访问服务器文件的内部搜索引擎,并在每个文本文件中查找客户端需要的关键字/短语。但是,使用迄今为止的代码,我不断收到此错误:

SearchFunction.java:28: error: cannot find symbol

          } while (fileName == true); 
^ symbol: variable fileName location: class SearchFunction

任何帮助都会很棒!谢谢!

public class SearchFunction{
public static void main (String[] args){

try{
do {
int count = 1;
String fileName = "Buyerserver" + count + "Log.txt";

BufferedReader br = new BufferedReader(new FileReader (fileName));

int linecount = 0;
String line;
System.out.println("Searching for " + args[0] + " in file...");

while ((line = br.readLine()) != null)
{
linecount++;
int indexfound = line.indexOf(args[0]);

if (indexfound > -1){
System.out.println("Word was found at positon " + indexfound + " on line " + linecount);
}
}
br.close();
count++;
} while (fileName == true);
}
catch (IOException e){
System.out.println("IO Error Occurred: " + e.toString());
}
}
}

最佳答案

String fileName

在循环内部声明,但您想在循环条件中测试它,但它不可见。

根据经验,变量在声明后在声明它们的 block 中可见。

例如,

if (x=0) // doesn't compile, x is not in scope here...
{
x=1; // doesn't compile, x is not in scope here...
int x;
x=2; // ok
}

也就是说,编写自己的搜索引擎的想法,特别是作为初学者,是非常值得怀疑的。

顺便说一下,fileName 永远不会为 true,因此您也必须修复 while 条件。

关于java - 错误: cannot find symbol - Internal Search Engine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24411787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com