简体   繁体   中英

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);

            }
        }

    }

Let's assume n > 2 is true once, then you read all the lines from your bufferedReaderStopList until you reach the EOF. This means that whenever n > 2 is true again the inner loop over bufferedReaderStopList will never be entered since readLine() always returns null from now on.

For starters your code needs to be structured better, add the content of bufferedReaderStopList into an array first at least. Also avoid doing split on the word string several times. Do it once and use the resulting array instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM