0

这个类中的最后一个 for 循环是罪魁祸首。我将模式字写入新创建的数组的位置。即使 eclipse 调试器显示值 i 小于 (tokens.length-2),for 循环也不会最后一次迭代。也许这是一个围栏问题,但我尝试了一个 do while 循环和一堆东西。此外,我已经发布了客户端代码和我正在使用的 txt 文件。

// This class creates an object wherein a text file is segmented and stored
// word for word in an array, facilitating a word count, the ability to check
// for the occurrence of a word and also the functionality of returning the 
// most frequently occurring words.

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class TextAnalysis14 {
    private String[] tokens;
    int maxNoOfWords;

    // Constructor that loads a file and assigns each word to an array index
    public TextAnalysis14 (String sourceFileName, int maxNoOfWords) throws FileNotFoundException{
        this.maxNoOfWords = maxNoOfWords;
        Scanner in = new Scanner(new FileReader(sourceFileName));
        String file = in.useDelimiter("\\Z").next();
        this.tokens = file.split ("[^a-zA-Z]+");
        in.close();
    }
    // Returns the number of words in the file.
    public int wordCount(){

        return tokens.length;

    }
    // Checks whether " word " is a word in the text.
    public boolean contains(String word){
        for(int i=0; i<tokens.length; i++){
            if(tokens[i].equalsIgnoreCase(word)){return true;}
        }
        return false;
    }
    // Returns the most frequent word(s) in lexicographical order.
    public String [] mostFrequentWords(){
        Arrays.sort(tokens);
        //Finds the mode word occurrence
        int wordValue=1;
        int maxValue=1;
        for(int i=0; i<tokens.length-2; i++){
            while(tokens[i].equalsIgnoreCase(tokens[i+1])){
                wordValue++;
                i++;
            }
            if(wordValue>maxValue){
                maxValue=wordValue;
            }
            wordValue=1;
        }
        //Determines length of return array 
        int numberOfModes=1;
        for(int i=0; i<tokens.length-2; i++){
            while(tokens[i].equalsIgnoreCase(tokens[i+1])){
                wordValue++;
                i++;
            }
            if(wordValue==maxValue){
                numberOfModes++;
            }
            wordValue=1;
        }
        //writes mode words to array
        int cursor =0;
        String[] modeWords = new String[numberOfModes];
        for(int i=0; i<tokens.length-2; i++){
            while(tokens[i].equalsIgnoreCase(tokens[i+1])){
                wordValue++;
                i++;
            }
            if(wordValue==maxValue){
                modeWords[cursor]=tokens[i];
                cursor++;
            }
            wordValue=1;
        }
        return modeWords;
    }

}

以下是我的客户端代码:

import java.io.FileNotFoundException;
import java.util.Arrays;

public class TextAnalysis_test01 {

    public static void main(String[] args) throws FileNotFoundException {

        TextAnalysis14 ta14 = new TextAnalysis14("testtext01.txt", 100);
        System.out.println(ta14.wordCount());
        System.out.println(ta14.contains("Bla"));
        System.out.println(ta14.contains("hello"));
        System.out.println(Arrays.toString(ta14.mostFrequentWords()));


    }

}

以下是我的txt文件的内容:

bla bla
dim dim 
dum dum

我得到输出:

6
true
false
[bla, dim, null]

很明显,据我所知,我没有向返回的字符串数组中的最后一个索引写入任何内容,因为最终的 for 循环没有最后一次迭代。我班上的部分评论为://将模式字写入数组。

任何帮助或建议都是天赐之物。干杯!

4

1 回答 1

0

我真的建议您重构代码以尝试改进它,但如果您只是在修复之后,以下更改应该会给出预期的输出:

int cursor =0;
    String[] modeWords = new String[numberOfModes];
    for(int i=0; i<=tokens.length-2; i++){
        while(i+1 <= tokens.length-1 && tokens[i].equalsIgnoreCase(tokens[i+1])){
            wordValue++;
            i++;
        }
        if(wordValue==maxValue){
            modeWords[cursor]=tokens[i];
            cursor++;
        }
        wordValue=1;
    }
    return modeWords;
}
于 2014-10-26T21:46:19.023 回答