STAGE 3 I (Word Histogram) Design and implement a program called \"WordHistogram
ID: 3891723 • Letter: S
Question
STAGE 3 I (Word Histogram) Design and implement a program called "WordHistogram.java" that creates a histogram that allows you to visually inspect the frequency distribution of a set of word in a given file. The program should read the input filename and output filename as command line arguments. A word is defined as collection of letters a-z and A-Z For example if the input file is: How much wood would a woodchuck chuck If a woodchuck could chuck wood? He would chuck, he would, as much as he could, And chuck as much wood as a woodchuck would If a woodchuck could chuck wood. The output file will contain: a: 4 and 1 as 4 chuck: 5 could: 3 he: 3Explanation / Answer
import java.io.*;
import java.util.*;
public class Demo203{
public static void main(String[] args){
String[] list = new String[100];
int[] freq = new int[200];
int count = 0;
for (int i = 0; i<200; i++)
freq[i] = 0;
try {
FileInputStream fin = new FileInputStream("input218.txt");
Scanner fc = new Scanner(fin);
while (fc.hasNextLine()){
String line = fc.nextLine();
line = line + " ";
line = line.replaceAll("[^a-zA-Z]+"," ").toLowerCase();
String[] list1 = line.split(" ");
for (int i = 0; i < list1.length; i++){
int found = 0;
for (int j = 0; j < count; j++){
if (list[j].equals(list1[i])){
found = 1;
freq[j]++;
}
}
if (found == 0){
list[count] = list1[i];
freq[count]++;
count++;
}
}
}
for (int i = 0; i<count; i++){
for (int j = i+1; j<count; j++){
if (list[i].compareTo(list[j]) > 0){
String temp = list[i];
int temp1 = freq[i];
list[i] = list[j];
freq[i] = freq[j];
list[j] = temp;
freq[j] = temp1;
}
}
}
for (int i = 0; i<count; i++){
System.out.println(list[i] + ":" + freq[i]);
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.