这个类中的最后一个for循环是罪魁祸首。我将模式字写入新创建的数组。即使eclipse调试器将值i显示为小于(tokens.length-2),for循环也不会迭代最后一次。也许这是一个fencepost问题,但我尝试过while 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循环不是最后一次迭代。我班上的部分评论://将模式字写入数组。
任何帮助或建议都是天赐之物。干杯!
答案 0 :(得分: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;
}