Here's the assignment:
As demonstrated in the example run above, an external text file name is entered first. Then any number of individual words are entered, all lower case, and separated by spaces. After doing its analysis, the program then displays a chart that gives the frequencies in the text of the indicated words, to four decimal places. Thus 2.42% of all of the words in the Heart of Darkness text file are "I", 1.33% are "he", and so forth. Pretty clearly Heart of Darkness is narrated in the first person, and is mostly about men.
Further requirements and tips:
* you MUST use an array of WordCount objects to keep track of the occurrences of the indicated words.
* Use printf from Chapter 5 to format your output.
* If s is a String, then this String class method call:
s.split(" "); // this is a single space surrounded by quote marks
I'm really close to finishing it but I keep getting a java.lang.NullPointerException error on line 27. Also, I didn't feel the need to post the echo class or driver class because they are pretty straight forward. Here's the wordCount class that he gave us:
public class WordCount{
private String word;
private int count;
public WordCount(String w){
word = w;
count = 0;
}
public String getWord(){
return word;}
public int getCount(){
return count;}
public void incCount(){count++;}
public String toString() {
return(word + " --- " + count);
}
public boolean equals(Object other){
WordCount i = (WordCount)other;
return (this.word.equals(i.word));
}
}
Here's what I have so far:
import java.io.IOException;
import java.util.*;
public class WordFreq extends Echo{
private String wordString;
private int ct = 0;
private String total = "";
private WordCount[]words = new WordCount[1000];
private String[]count;
private String[]wordString2;
private String s;
public WordFreq(String f, String w)throws IOException{
super(f);
wordString = w;
}
public void processLine(String line){
wordString2 = wordString.split(" ");
s = line;
total += s + " ";
count = total.split(" ");
for(int j = 0; j < count.length; j++){
words[j] = new WordCount(count[j]);
}
for(int j = 0; j < wordString2.length; j++){
if(wordString2[j].equals(words[j].getWord())){ //
words[j].incCount();
}
}
}
public void reportFrequencies(){
for(int j = 0; j < wordString2.length; j++){
System.out.println(wordString2[j] + words[j].getCount()); // USE PRINTF HERE ONCE IT WORKS
}
}
}
EDIT: Just realized my if statement in the processline method doesn't look through all the words in the text. I'm thinking I'll have to create a for loop instead a while loop.