Java 如何将键入的字符串与从文件读取的数组中的字符串进行比较?
我用java编写了一些代码,能够逐行读取文本文件,然后将这些行拆分为单词/标记,并将它们存储在数组中。我希望我正在编写的程序允许我输入一些文本,并将其与存储的令牌数组进行比较。如果有匹配项,我希望程序返回匹配字符串并打印出来,以及匹配字符串前后的一些字符串。下面是我正在使用的一段代码。请告诉我哪里出了问题,并分享可能帮助我解决此问题的任何链接或示例。非常感谢Java 如何将键入的字符串与从文件读取的数组中的字符串进行比较?,java,arrays,string,text-files,token,Java,Arrays,String,Text Files,Token,我用java编写了一些代码,能够逐行读取文本文件,然后将这些行拆分为单词/标记,并将它们存储在数组中。我希望我正在编写的程序允许我输入一些文本,并将其与存储的令牌数组进行比较。如果有匹配项,我希望程序返回匹配字符串并打印出来,以及匹配字符串前后的一些字符串。下面是我正在使用的一段代码。请告诉我哪里出了问题,并分享可能帮助我解决此问题的任何链接或示例。非常感谢 //declare variables that will be involved in Find operation St
//declare variables that will be involved in Find operation
String equiptype;
String equipid;
String[] compareid;
String[] comparetype;
// Allow user to type in what they are looking for.
System.out.println ("Enter the equipment type: ");
equiptype = TextIO.getlnString();
System.out.println("Enter the equipment id: ");
equipid = TextIO.getlnString();
//System.out.println("You searched for: " + equiptype + " " + "and" + " " + equipid); // testing the getlnString statements
//comparing strings typed in to string array tokens to find a match
if (equiptype == null && equipid != null)
compareid [0]= tokens[0];
for(int j=0;j < tokens.length;j++){
compareid[k]=tokens[k];
if(!compareid.equals(equipid)) return false;
else
return compareid[j];
System.out.println("Result: " + tokens[j-1] + " " + compareid [j] + " " + tokens [j+1] + " " + tokens[j+2] + " " + tokens[j+3]);
}
else if (equipid == null && equiptype != null)
{
comparetype [0]= tokens[0];
for(int k=0;k < tokens.length;k++){
if(!comparetype.equals(equiptype)) return false;
comparetype = tokens[k];
return comparetype[k];
System.out.println("Result: " + comparetype[k] + " " + tokens[k+1] + " " + tokens[k+2] + " " + tokens[k+3] + " " + tokens[k+4]);
}
else {
System.out.println("Please Enter the equipment name and/or id or click the 'List All' button.");
//声明将参与查找操作的变量
字符串类型;
弦设备;
字符串[]比较ID;
String[]comparetype;
//允许用户键入他们正在查找的内容。
System.out.println(“输入设备类型:”);
equiptype=TextIO.getlnString();
System.out.println(“输入设备id:”);
equipid=TextIO.getlnString();
//System.out.println(“您搜索的是:“+equiptype++”和“+”+equipid”);//测试getlnString语句
//将键入的字符串与字符串数组标记进行比较以查找匹配项
if(equiptype==null&&equipid!=null)
compareid[0]=令牌[0];
对于(intj=0;j
compareid[0]=tokens[0];-令牌从何而来?此comparetype[0]=tokens[0];
必须是comparetype[0]。等于(令牌[0]);
,因为这是一种字符串类型。compareid[0]=tokens[0];
@Jens根据Java版本的不同,这是不正确的。定义少量(前后)。以相同的方式使用其他输入。打印(currentIndex-now,currentIndex+now);