Question:
Java Programing Help - Word Frequencies in a File?
Justin
2013-04-17 11:40:05 UTC
I have a programming assignment for my Java class and I cannot figure out what is wrong!

Assignment:

For this assignment you are to write a program that handles text files. The assignment requires three classes (plus library classes); we've given you two - FreqStudy, the driver, and another class, WordCount.

FreqStudy:
import java.util.*;
import java.io.*;

public class FreqStudy{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
System.out.println("enter file name");
String fileName = scan.next();
Scanner scan2 = new Scanner(System.in);
System.out.println("enter words to search for");
System.out.println("enter lower case, separated by spaces");
String wordString = scan2.nextLine();
WordFreq f = new WordFreq(fileName,wordString);
f.readLines();
f.reportFrequencies();
}
}

WordCount:
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));
}
}


Your job is to write WordFreq, the heart of the application. It MUST extend the Echo class (from Ch 9 of text):

Echo:

import java.util.Scanner;
import java.io.*;

public class Echo{
String fileName; // external file name
Scanner scan; // Scanner object for reading from external file

public Echo(String f) throws IOException
{
fileName = f;
scan = new Scanner(new FileReader(fileName));
}

public void readLines(){ // reads lines, hands each to processLine
while(scan.hasNext()){
processLine(scan.nextLine());
}
scan.close();
}

public void processLine(String line){ // does the real processing work
System.out.println(line);
}
}

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

returns an array of strings that consists of the tokens (separated by white space) in s. For example, given
String s = "i he his she hers";
String[] words = s.split(" ");


So, here is what I have for code so far for my WordFreq class:

import java.io.IOException;
import java.util.*;

public class WordFreq extends Echo{
private String name;
private String wordString;
private int total;
private String[] words;

public WordFreq(String fileName, String word){
super(fileName);
wordString = word;
}

public void splitLine(){
String s = wordString;
words = s.split(" ");
}

public void countWords(){
StringTokenizer st = new StringTokenizer(name);
while (st.hasMoreTokens()){
for(int j = 0; j < words.length; j++){
total++;
if (st.nextToken() == words[j])
words[j].incCount();
}
}
}

public void reportFrequencies(){
for (int j = 0; j < words.length; j++){
System.out.println(words[j].getWord() + " " + (words[j].getCount()/total));
}
}
}

I am getting errors on line 26 and two on line 33. It cannot find the incCount(), getWord(), and getCount() methods.

Any help would be greatly appreciated!
Three answers:
godfatherofsoul
2013-04-17 12:52:25 UTC
Those methods are on WordCount, but you're calling those methods on an element of words which is a String array.
?
2013-04-17 23:09:09 UTC
You can't use these commands on something that is not a WordCount object. They are specific to the WordCount class and therefore are only meant for those WordCount objects. What you should do is create a WordCount object, mainly an array, and then use those commands on the WordCount object.
John
2013-04-19 21:56:18 UTC
fileName = FreqStudy.java being used to test;

keyword = public scanner system

The output result is listed below...

2/70 public 0.0286

4/70 scanner 0.0571

5/70 system 0.0714



import java.io.*;

import java.util.*;



public class WordFreq extends Echo {

private String name;

private String keyword;

private int totalCount = 0;

private List words = new ArrayList();



public WordFreq(String fileName, String word) throws IOException {

super(fileName);

this.keyword = word;

}



public void reportFrequencies() {

load();

report();

}



private void load() {

try {

Scanner fileIn = new Scanner(new FileReader(super.fileName));

while (fileIn.hasNext()) {

String[] tokens = fileIn.nextLine().split("[^\\w]");

for (int i = 0; i < tokens.length; i++) {

if (tokens[i].length() > 0)

count(tokens[i]);

}

}

} catch (IOException ex) {

ex.printStackTrace();

}

}



private void count(String word) {

boolean found = false;

WordCount wc = new WordCount(word.toLowerCase());

// the constructor of WordCount should be changed to 1 instead of 0.

wc.incCount();

for (WordCount wordCount : words) {

if (wordCount.equals(wc)) {

wordCount.incCount();

found = true;

break;

}

}



if (!found) {

words.add(wc);

}

totalCount++;

}



private void report() {

for (WordCount wc : words) {

if (isKeyword(wc.getWord())) {

double freq = wc.getCount() / (double) totalCount;



// for debug, uncomment the next line...

// System.out.printf("%d/%d ", wc.getCount(), totalCount);



System.out.printf("%s %.4f%n", wc.getWord(), freq);

}

}

}



private boolean isKeyword(String word) {

boolean result = false;

for (String s : keyword.split(" ")) {

if (word.equalsIgnoreCase(s)) {

result = true;

break;

}

}

return result;

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...