我需要创建一个程序来计算文件中的单词数量和唯一单词的数量……我已经想出了如何计算单词的数量,但我在计算唯一单词部分时遇到了麻烦。这是我的统计总字数的方法。
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;发布于 2014-03-12 09:13:00
嗯,这是一项荒谬的任务,而且通常是一种糟糕的实现任务的方式。
无论如何,这里有一些伪代码和一个简单的方法定义,可以用来解决任务-它有可怕的复杂性界限和固定的限制,但它应该足以开始一个。
// 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 onehttps://stackoverflow.com/questions/22339946
复制相似问题