Reputation: 26
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;
Upvotes: 0
Views: 1339
Reputation: 61875
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
Upvotes: 1