[英]Search string within a text file
I'm trying to search a text file for a set of keywords. 我正在尝试在文本文件中搜索一组关键字。
I put them as a string and I'm having a problem with the if
statement. 我将它们作为字符串放置,并且if
语句出现问题。
On this line: 在这行上:
if(s.contains (keywords)) {
I didn't have it before. 我以前没有 It says to do the following: 它说要执行以下操作:
The method contains(CharSequence) in the type String is not applicable for the arguments (String[])) 字符串类型中的方法contains(CharSequence)不适用于参数(String []))
But that just changes the String to CharSequence
, still resulting in an error! 但这只是将String更改为CharSequence
,仍然导致错误!
Here is the code: 这是代码:
import java.io.*;
public class SearchTextFile {
public static void main(String args[]) throws Exception {
int tokencount;
FileReader fr = new FileReader("c:\\searchtxt.txt");
BufferedReader br = new BufferedReader(fr);
String s;
int linecount = 0;
String[] keywords = {
"AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:"
};
String line;
while ((s = br.readLine()) != null) {
if (s.contains(keywords)) {
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
}
}
}
Since String does not have the method String.contains(String)
, you can achieve it as follows: 由于String没有方法String.contains(String)
,因此可以按以下方式实现它:
Change your keywords array to ArrayList<String>
. 将关键字数组更改为ArrayList<String>
。 Then as you read a line , get all the words in an array using String.split()
method. 然后,当您读一行时,使用String.split()
方法获取数组中的所有单词。 Now you can loop through the word array ( created by reading the line from file) and check if the keywordList contains the word or not. 现在,您可以遍历单词数组(通过从文件中读取行来创建),并检查keywordList是否包含单词。 Note: KeywordList.contains(s)
will be true
if s
is exactly as a keyword. 注意:如果s
与关键字完全相同,则KeywordList.contains(s)
将为true
。 It will produce false
if s
is a string with other words but it contains one or more elements from keywords
array .Thus this test will not produce an effective search result. 如果s
是包含其他单词的字符串,但它包含keywords
array中的一个或多个元素,则它将生成false
s
因此,此测试将不会产生有效的搜索结果。 The intention of the search is to check any input line s
if it has at least one of the keywords from the keywords
array. 搜索的目的是检查任何输入行s
是否具有keywords
数组中的至少一个keywords
。 So one such solution can be as follows: 因此,一种解决方案如下:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class SearchTextFile
{
public static void main(String args[]) throws Exception
{
int tokencount;
FileReader fr = new FileReader("c:\\searchtxt.txt");
BufferedReader br = new BufferedReader(fr);
String s = "";
int linecount = 0;
ArrayList<String> keywordList = new ArrayList<String>(Arrays.asList("AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:"));
String line;
while ((s = br.readLine()) != null)
{
String[] lineWordList = s.split(" ");
for (String word : lineWordList)
{
if (keywordList.contains(word))
{
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
break;
}
}
}
}
}
A more efficient way will be to store the list of keywords in a Set and also the list of tokens on each line in a set. 一种更有效的方法是将关键字列表存储在集合中 ,还将标记列表存储在集合中的每一行上。 This prevents the inefficiency involved in iterating over all elements of the list. 这样可以避免遍历列表中所有元素的效率低下。 With that in mind, I've modified your code slightly: 考虑到这一点,我对您的代码进行了一些修改:
public static void main(String args[]) throws Exception
{
int tokencount;
FileReader fr=new FileReader("c:\\searchtxt.txt");
BufferedReader br=new BufferedReader(fr);
String s;
int linecount=0;
//String[] keywords = { "AB", "BC","D1", "B11", "BL:", "B/1:","B1L:", "B11:"};
Set<String> keywords = new HashSet<>(Arrays.asList("AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:"));
String line;
Set<String> lineSet;
while ((s=br.readLine())!=null) {
lineSet = new HashSet(Arrays.asList(s.split(" ")));
if(!Collections.disjoint(lineSet, keywords)) { //returns true if both sets have no elements in common;
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
}
}
You cannot use a String array ( String[]
) with contains
, use a regex instead, ie: 您不能将String数组( String[]
)与contains
使用,而应使用正则表达式来代替,即:
import java.util.regex.*;
if (subjectString.matches("AB|BCD1|B11|BL:|B/1:|B1L:|B11:")) {
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
There is no such a method as contains(String[]). 没有contains(String [])这样的方法。 The correct one is contains(String) 正确的是contains(String)
like @Pedro Lobito said, you could use regex. 就像@Pedro Lobito所说的,您可以使用正则表达式。 Also you could loop through your string array perhaps, 您也可以遍历字符串数组,
while ((s=br.readLine())!=null) {
for (String str: keywords) {
if(s.contains (str)) {
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.