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.java
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 class
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)); } }
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(" ");
Then words is this five element array of strings: {i, he, his, she, hers}
At the course website we've provided some tips for solving this problem, and in addition we've provided some text files that you might want to experiment with. Thus we urge you to check the ProgramNotes link at the course website for further information about the assignment.
In the box below, enter your WordFreq code. Be sure to comment your code. (As usual, do not add/use additional import statements)
This is the code I have this far but I know that it is wayyy offfimport 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[8]; 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(); } } }
CAN'TTT even begin to explain how confused I am. Someone please help!