Reputation: 37
The last for loop in this class is the culprit. Where i write the mode words to the newly created array. The for loop will not iterate a final time, even though the eclipse debugger shows the value i as being lesser than (tokens.length-2). Perhaps it is a fencepost problem, but i have tried a do while loop and a bunch of stuff. Further i have posted the client code and the txt file i am using.
// 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;
}
}
The following is my client code:
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()));
}
}
The following is the content of my txt file:
bla bla
dim dim
dum dum
And i get the output:
6
true
false
[bla, dim, null]
As is evident i'm not writing anything to the last index in the returned string array, as far as i can tell, because the final for loop is not iterating a last time. The part in my class commented with: //writes mode words to array.
Any help or advice would be a godsend. Cheers!
Upvotes: 0
Views: 82
Reputation: 344
I would really advise you to refactor your code in an attempt to improve it but if you are only after a fix, the following change should give the output expected:
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;
}
Upvotes: 0