为什么我的 if 条件不起作用 (if (n>2))?
why my if condition does not work (if (n>2))?
//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 再次为真时,将永远不会进入 bufferedReaderStopList 的内部循环,因为 readLine() 从现在开始总是 returns null。
对于初学者,您的代码需要更好地结构化,至少首先将 bufferedReaderStopList 的内容添加到数组中。还要避免多次对单词字符串进行拆分。执行一次并改用结果数组。
//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 再次为真时,将永远不会进入 bufferedReaderStopList 的内部循环,因为 readLine() 从现在开始总是 returns null。
对于初学者,您的代码需要更好地结构化,至少首先将 bufferedReaderStopList 的内容添加到数组中。还要避免多次对单词字符串进行拆分。执行一次并改用结果数组。