Create the class “CountWords” with a main() method that reads a text file, “inpu
ID: 3764132 • Letter: C
Question
Create the class “CountWords” with a main() method that reads a text file, “input.txt”, and counts how many times each word appears and writes those results to another text file, “output.txt”. Do not specify the folder path for files, just use the File(“input.txt”) and File(“output.txt”) constructors. Words must begin with a letter, so 999SO-FINE would not be a word. Words are not case sensitive, so SPOON, spoon, and Spoon all count as the same word. Once your program finishes counting all words, it will write to a text file “output.txt” with one line for each word counted in the order in which the words were first found. Each line should start with the count, right justified in 3 spaces, and follow with a space followed by the word converted to lowercase. IN JAVA
Explanation / Answer
Hi,
Below is the program that you are looking for:
Program:
package java_project;
import java.io.*;
public class Countwords {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
try
{
FileReader fi = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fi);
String string;
while((string = br.readLine()) != null)
countnoofwords(string);
fi.close();
}
catch(FileNotFoundException ex)
{
System.out.println("File Not Found");
}
catch(IOException ex)
{
System.out.println("Exception : " + ex);
}
}
static void writedatatofile(String arr[],int integerarr[],int length)
{
try
{
FileOutputStream fo = new FileOutputStream("output.txt");
FileOutputStream fo1 = new FileOutputStream("output1.txt");
DataOutputStream dos = new DataOutputStream(fo);
int i,j,value;
String temp;
for(i=0;i<length;i++)
{
dos.writeChars(arr[i]);
dos.writeChars(Integer.toString(integerarr[i]));
dos.writeChar(' ');
}
fo.close();
dos = new DataOutputStream(fo1);
for(i=0;i<length;i++)
{
for(j=i+1;j<length;j++)
{
if(integerarr[i] < integerarr[j])
{
value = integerarr[j];
integerarr[j] = integerarr[i];
integerarr[i] = value;
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for(i=0;i<length;i++)
{
dos.writeChars(arr[i]);
dos.writeChars(Integer.toString(integerarr[i]));
dos.writeChar(' ');
}
fo1.close();
}
catch(IOException e)
{
System.out.println("Exception : " + e);
}
}
static void countnoofwords(String str)
{
int length,total_words=1,frequencyarray[];
int i,j=-1,k=0,temp;
int count=0,flag=0;
length = str.length();
char cha[];
String stringarr[],string1[];
for(i=0;i<length-1;i++)
{
if(str.charAt(i) != ' ' && str.charAt(i+1) == ' ')
total_words++;
}
stringarr = new String[total_words+1];
frequencyarray = new int[total_words];
string1 = new String[total_words];
for(i=0;i<length-1;i++)
{
if(str.charAt(i) != ' ' && str.charAt(i+1) == ' ')
{
cha= new char[(i+1) - j];
str.getChars(j+1,i+1,cha,0);
stringarr[k++] = new String(cha);
j = i+1;
}
}
cha= new char[(i+1) - j];
str.getChars(j+1,i+1,cha,0);
stringarr[k] = new String(cha);
k=0;
for(i=0;i<total_words;i++)
{
temp = 1;
flag = 0;
for(j=i+1;j<=total_words;j++)
{
if(stringarr[i].equals(stringarr[j]))
temp++;
}
frequencyarray[k] = temp;
for(count=0;count<k;count++)
{
if(stringarr[i].equals(string1[count]))
flag = 1;
}
if(flag == 0)
string1[k++] = stringarr[i];
}
for(i=0;i<k;i++)
System.out.println(string1[i] + " " + frequencyarray[i]);
writedatatofile(string1,frequencyarray,k);
}
}
Below is the explaination:
1.This program has a main method that calls methods countnoofwords to count the number of time that a particular word appears in the text file which will be taken from input.txt.
2.Create this input.txt with some words as:
create
words
to
count
count
And then save the file.
3.Finally it counts the number of times each word occurs in the input file and prints that value to another file say output.txt .
Hope that helps...HAPPY ANSWERING!!!!!!!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.