我正在读取文件中的停止词,我将它保存在HashSet
中。我比较说HashSet
与String
检查停用词。字符串没有正确检查停止词
如果我在String
-变量中放置了一个单词,例如“the”,那么我的输出是“Yes”。但是,如果我将“Apple is it”或“它是一个苹果”这样的东西输出为“No”,尽管String
-variables包含停用词。
这里的整个程序,包含两种方法,一种用于读取文件和一个用于去除停止词:
private static HashSet<String> readFile(){
Scanner x = null;
HashSet<String> hset = new HashSet<String>();
try {
x = new Scanner(new File("StopWordsEnglish"));
while(x.hasNext()){
hset.add(x.next());
}
} catch(Exception e) {
e.printStackTrace();
} finally {
x.close();
}
return hset;
}
public static void removeStopWords(){
HashSet<String> hset = readFile();
System.out.println(hset.size());
System.out.println("Enter a word to search for: ");
String search = "is";
String s = search.toLowerCase();
System.out.println(s);
if (hset.contains(s)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
使用调试器,并发现它在空间 – Jens