简体   繁体   中英

How to count the number of unique words in a text file? Not allowed to use Hash

I need to create a program that counts the number of words in a file and the number of unique words....I've figured out how to count the number of words but I'm having trouble figuring out the unique words part. This is my counting total words method.

public static int numberWord(File f) throws FileNotFoundException{
    Scanner welcome = new Scanner(f);
    int count=0;
    while(welcome.hasNext()){
        String word=welcome.next();
        count++;
    }
    return count;

Well, this is a ridiculous assignment and generally a terrible way to implement this task ..

Anyway, here is some pseudo-code and a simple method definition that can be used to solve the task - it has horrid complexity bounds and a fixed limit, but it should be enough to get one started.

// Find the index of the word within an array.
// If the word currently is not in the array, then
// add it and return the index the word was added at.
int ensureWord(String[] words, String word) {
    for each index in words do
        if the word is equal() to the word at the index then
            // existing word found
            return the index
        if (null) is at the index then
            // "found the end" of the known words, so must be a new word
            assign word to the words array at the index
            return the index

    throw terrible exception, word not ensured
}

String words[] = new String[MAX_DISTINCT_WORDS_ALLOWED];
String counts[] = new int[MAX_DISTINCT_WORDS_ALLOWED];

for each word in the file do
   find/ensure the index in words by invoking ensureWord(words, word)
   and increment the count at the index by one

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