Please write a \"Concordance\" Class that contains, the unique word list, freque
ID: 3763417 • Letter: P
Question
Please write a "Concordance" Class that contains, the unique word list, frequency for each word, total word count, and a unique word total from a source input file. The text file I am going to use in bush's inaugral address, which I already have set up. I just need to code to complete this program. I have the following code but it keeps giving me too many errors.
import java.util.*;
import java.io.File;
public class Labb7 {
public static void main(String[] args) throws Exception {
String[] words = new String[100000000];
int[] freq = new int[100000000];
int count = 0;
int unique = 0;
inputWords();
sortWordsbyFrequency();
printWords();
System.out.println("The word count is: " + count);
System.out.println("The unique word count is: " + unique);
}
//This method will input a string of text from the text file given.
public static void inputWords() throws Exception {
File sourceFile = new File("bush.txt");
Scanner sourceScanner = new Scanner(sourceFile);
String word;
int i;
words[unique] = sourceScanner.next();
count = 1;
unique = 1;
freq[0] = 1;
while (sourceScanner.hasNext()){
word = sourceScanner.next();
count = count + 1;
i = 0;
while (i<unique&&!words[i].equals(word)){
i = i + 1;
}
if (i==unique)
{words[unique] = word; unique++; }
freq[i]++;
}
}
//This method will sort the string by number of unique words in frequency order.
public static void sortWordsbyFrequency() {
int i;
int pass;
int small;
String wordtemp;
int freqtemp;
for (pass=0;pass<unique; pass++){
small = pass;
for (i=pass;i<unique; i++)
if (freq[i]<freq[small])
small = i;
wordtemp = words[pass];
freqtemp = freq[pass];
words[pass] = words[small];
freq[pass] = freq[small];
words[small] = wordtemp;
freq[small] = freqtemp;
}
}
//This method will print the unique words in order of frequency.
public static void printWords() {
int i=0;
while (i<ucount){
System.out.printf("%5d %-15s %8d %5d %-15s %8d %5d %-15s %8d ",i,words[i],freq[i],i+1,words[i+1],freq[i+1],
i+2,words[i+2],freq[i+2]);
i=i+3;
}
System.out.println("------------------------------------------------");
}
}
Explanation / Answer
Here is your running code:
import java.util.*;
import java.io.File;
public class Labb7
{
String[] words = new String[100];
int[] freq = new int[100];
int count = 0;
int unique = 0;
public void inputWords() throws Exception
{
File sourceFile = new File("bush.txt");
Scanner sourceScanner = new Scanner(sourceFile);
String word;
int i;
words[unique] = sourceScanner.next();
count = 1;
unique = 1;
freq[0] = 1;
while (sourceScanner.hasNext())
{
word = sourceScanner.next();
count = count + 1;
i = 0;
while (i<unique&&!words[i].equals(word))
{
i = i + 1;
}
if (i==unique)
{
words[unique] = word;
unique++;
}
freq[i]++;
}
}
public void sortWordsbyFrequency()
{
int i;
int pass;
int small;
String wordtemp;
int freqtemp;
for (pass=0;pass<unique; pass++)
{
small = pass;
for (i=pass;i<unique; i++)
{
if (freq[i]<freq[small])
{
small = i;
}
}
wordtemp = words[pass];
freqtemp = freq[pass];
words[pass] = words[small];
freq[pass] = freq[small];
words[small] = wordtemp;
freq[small] = freqtemp;
}
}
public void printWords()
{
int i=0;
while (i<this.count)
{
System.out.printf("%5d %-15s %8d %5d %-15s %8d %5d %-15s %8d ",i,this.words[i],this.freq[i],i+1,this.words[i+1],this.freq[i+1],i+2,this.words[i+2],this.freq[i+2]);
i=i+3;
}
System.out.println("------------------------------------------------");
}
public static void main(String[] args) throws Exception
{
Labb7 l7=new Labb7();
l7.inputWords();
l7.sortWordsbyFrequency();
l7.printWords();
}
}
If you have any doubt you can ask anytime