Hi guys, Im currently working on a java project but ive come across a small minor problem. I have 4 main classes that im working with. TextReader, WordCount, WordCollecter and DisplayWords.
The TextReader class reads an input file and a method called readNextWord will return the next word from the input file. The WordCount class provides a way of keeping track of the frequency of a particular word from the input text. The WordCollector class will use the TextReader to read in the set of words contained in the input text. The DisplayWords which will display the words on the canvas.
My main problem is with the WordCollector class, here is the code i have for it so far:
import java.util.ArrayList;
import java.util.Collections;
public class WordCollector
{
private TextReader reader;
private ArrayList<WordCount> countWords;
private DisplayWords display;
private WordCount newWord;
public WordCollector (TextReader text){
this.reader = text;
countWords = new ArrayList<WordCount>();
display = new DisplayWords();
getWordCount();
find();
}
private void getWordCount(){
while (reader.readNextWord()!=null){
String count = reader.readNextWord();
newWord = new WordCount(count);
countWords.add(newWord);
}
}
private WordCount find(){
String nextWord = reader.readNextWord();
WordCount word = new WordCount(nextWord);
if(countWords.contains(word)){
word.incrementCount();
return word;
}else{
countWords.add(word);
}
Collections.sort(countWords);
return word;
}
public void printReport(){
System.out.println("there are " + countWords.size() + " words");
for (int i=0; i<countWords.size(); i++){
System.out.println(countWords.get(i).getText().toString() + " appears " + countWords.get(i).getCount() + " times");
}
}
}
The input file for the TextReader class is the Humpty Dumpty story:
Humpty Dumpty sat on a wall,
Humpty Dumpty had a great fall.
All the king's horses and all the king's men
Couldn't put Humpty together again
When i call the PrintReport method the count doesnt work out exactly how it should be, Here is an example of the output:
there are 10 words
Text@44ac8dff appears 1 times
Text@1c9e8392 appears 1 times
Text@6acff4eb appears 1 times
Text@7ad8a715 appears 1 times
Text@25c192b5 appears 1 times
Text@4786bc70 appears 1 times
Text@5ebd81bf appears 1 times
Text@6b915330 appears 1 times
Text@11ecab7c appears 1 times
Text@48ee6315 appears 1 times
It should look like this:
There were 17 words.
humpty appeared 3 times.
dumpty appeared 2 times.
king appeared 2 times.
sat appeared 1 times.
wall appeared 1 times.
great appeared 1 times.
fall appeared 1 times.
horses appeared 1 times.
men appeared 1 times.
couldn appeared 1 times.
put appeared 1 times.
together appeared 1 times.
again appeared 1 times.
Issues that need to be sorted:
1) I need to be able to show the actual word itself and how many times it appears.
2) why am i getting hexadecimal values?
Thanks guys.