Java 错误:找不到符号-内部搜索引擎
我需要创建一个访问服务器文件的内部搜索引擎,并在每个文本文件中查找客户端需要的关键字/短语。但是,使用到目前为止的代码,我一直遇到以下错误: SearchFunction.java:28:错误:找不到符号Java 错误:找不到符号-内部搜索引擎,java,compiler-errors,search-engine,Java,Compiler Errors,Search Engine,我需要创建一个访问服务器文件的内部搜索引擎,并在每个文本文件中查找客户端需要的关键字/短语。但是,使用到目前为止的代码,我一直遇到以下错误: SearchFunction.java:28:错误:找不到符号 } while (fileName == true); ^ symbol: variable fileName location: class SearchFunction 任何帮助都会很棒!谢谢 public clas
} 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());
}
}
}
在循环内声明,但您希望在循环条件下测试它,在循环条件下它不可见
根据经验,变量在声明后在声明它们的块中是可见的
比如说,
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概念和调试技能。我认为你应该放慢速度。改用solr,它会解决你的问题,因为@SotiriosDelimanolis u r缺少基本的java。在接受开发java文件之前,你应该开始阅读一些java书籍或其他东西。。。。但问题是。文件名在循环中。它应该在外面定义(在做之前)。也(文件名是string,您无法将其与true进行比较。您可能想检查该文件是否存在?使用fileName==true
,您试图实现什么?您正在将字符串计算为布尔值,我认为这不是您想要的。此外,您还想用较差的java知识完成一项复杂的任务。我强烈建议您通过在开始类似的内容之前,请先学习一些java教程。谢谢大家的帮助!+1-关于变量范围的一两句话会让这个答案更好。@Rudi好的,扩展我的答案
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
}