我正在尝试对文本分析程序进行编码,其中一种方法应该使用文本文件作为输入来编写一个文件,其中包含由某个字母开始的每个单词以及该单词的出现次数({ {1}}等等。
如果输入是:“O Pai Natal nao existe。 Mas o coelho da Pascoa existe (e Fada dos Dentes tambem)。“
e的输出应为:EXISTE 2E 1
到目前为止,我的代码如下 - 我知道我可以使用BufferedReader
,但这是学校工作,我们还没有学到这一点,所以我试图将它保持在讲座内 - :< / p>
HashMap
我也在使用一些可能已经注意到的辅助方法:
void WordList(char c, String file) throws IOException {
BufferedWriter out = new BufferedWriter(new FileWriter(ficheiro+".txt"));
String[] words = null;
int indexVector = 0;
String[] wordAlreadyWritten = null;
int indexVector2 = 0;
for (int i = 0; i < text.length; i++) {
if (text[i].charAt(0) == c) {
words[indexVector] = texto[i]; /*error here */
indexVector++;
}
}
for (int i = 0; i < words.length; i++) { /*error here*/
if (!wordWasAlreadyWritten(words[i], wordAlreadyWritten)) {
out.write(words[i] + ": " + countNumberRepetitions(words[i], words));
wordAlreadyWritten[indexVector2] = words[i]; /* error here*/
indexVector2++;
}
}
out.close();
}
如果有人能够解释static boolean wordWasAlreadyWritten(String original, String[] array) {
boolean was = false;
for (int j = 0; j < array.length; j++) {
if (original.equals(array[j])) {
was = true;
}
}
return was;
}
static int countNumberRepetitions(String word, String[] array) {
int counter = 1;
for (int i = 0; i < array.length; i++) {
if (word.equals(array[i])) {
contador++;
}
}
return counter;
}
我正在解决的问题以及如何解决问题,我会非常感激。此外,如果有人知道如何使我的代码更好,也将非常感激。
答案 0 :(得分:2)
初始化String数组
String[] words = new String[count]; // count any integer value
String[] wordAlreadyWritten = new String[count]; // count any integer value
因此您将面临错误。
答案 1 :(得分:0)
几个Q:
另一个建议:
你还没有研究过HashMap,但你仍然可以编写自己喜欢的HashMap或与之非常相似。我相信你会从中学到一些东西。