//this code is to compare two files and delet stop list word from file algorithm
FileReader reader = new FileReader("C:\\Users\\Sara\\Desktop\\IRP\\Information Retrieval\\Algorithm.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
FileReader readerStopList = new FileReader("C:/Users/Sara/Desktop/IRP/stopwords2.txt");
BufferedReader bufferedReaderStopList = new BufferedReader(readerStopList);
String word, stopword, newWord = "";
while ((word = bufferedReader.readLine()) != null) {
for (int k = 0; k < word.split(" ").length; k++) {
int count = 0;
newWord = word.split(" ")[k];
int n = newWord.length();
if (n > 2) { //this statment to skip words of length 2
while ((stopword = bufferedReaderStopList.readLine()) != null) {
for (int j = 0; j < stopword.split(" ").length; j++) {
if (newWord.equalsIgnoreCase(stopword.split(" ")[j])) {
count++;
}
}
}
if (count == 0) {
System.out.println(newWord);
}
}
}
最佳答案
假设n> 2一次为真,那么您从bufferedReaderStopList中读取所有行,直到到达EOF。这意味着只要n> 2再次为true,就永远不会进入bufferedReaderStopList的内部循环,因为readLine()从现在开始总是返回null。
对于初学者,您的代码需要更好地结构化,至少应首先将bufferedReaderStopList的内容添加到数组中。还要避免对单词字符串进行多次拆分。只需执行一次,然后使用结果数组。
关于java - 为什么我的if条件不起作用(if(n> 2))?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49140206/