Can someone help add on this action into my code?: The program should remove all
ID: 3691770 • Letter: C
Question
Can someone help add on this action into my code?:
The program should remove all capitalization, sort the words in alphabetical order, and write the resulting list of words back to another file.
Although the input file may contain the same word multiple times, the output file should only contain one instance of each word.
Upon completion, your program must print the number of words read, and the number of words written back to standard output, as well as the names of the files read from and written to.
I would be really appreciate if someone can do this.
This is the code I have so far:
package readdatafromfile;
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
/**
*
* @author Admin
*/
public class FileRead {
static int wordCount = 0;
static int uniqWC = 0; // unique word count
static String[] words = new String[1000];
static int matched = 1;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//OrderedSet setOfWords;
//setOfWords = new OrderedSet();
String ln = null;
try {
FileReader fr = new FileReader("ipfile.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("opfile.txt");
BufferedWriter bw = new BufferedWriter(fw);
while ((ln = br.readLine()) != null) {
bw.write(ln);
bw.newLine();
System.out.println(ln);
wordCount++;
for (int i = 0; i < wordCount; i++) {
if (words[i] == null)
words[i] = ln;
if (words[i] != null)
if (words[i].equals(ln)) {
matched = 1;
break;
}
if (matched == 0) {
words[wordCount - 1] = ln;
uniqWC++;
}
} // end for
}
bw.close();
br.close();
} // end try
catch (IOException errIO) {
errIO.printStackTrace();
} // end catch
System.out.println("From the inut file of ipfile.txt, Total words read = " + (wordCount));
System.out.println("Total unique words written to file opfile.txt: " + (uniqWC));
System.out.println("Unique words are: " + Arrays.toString(words));
}
}
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package menu;
import java.io.File;
import java.io.FileNotFoundException; // importing required classes
/**
*
* @author Shivakumar
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter; // importing different kind of classes
import java.io.IOException;
import java.util.Arrays;
public class Menu {
public static void main(String args[]) throws FileNotFoundException, IOException
{
File f=new File("e:/Hello.txt"); // file object for file to wrtie
FileWriter fw=new FileWriter(f); // file wrtier for buffered reader class
if(!f.exists()) // if file does not exists already creating a new file
f.createNewFile();
BufferedWriter bw; //buffered writer object creation
bw = new BufferedWriter(fw); // initializing object of filewrtier
BufferedReader br=new BufferedReader(new FileReader("c:/filer.txt")); //object to read file
String line,nw=null;
int count=0; // count to find number of words
while((line=br.readLine())!=null) // while for reading till last line of the file
{
System.out.println(line);
String l=line.toLowerCase(); // converting to lowercase
String[] arr=l.split(" "); // split method to find words and sort them
Arrays.sort(arr); // sorting array of strings
for(int i=0;i<arr.length;i++) // loop to append sorted words
{
nw=nw+arr[i];
}
count=count+arr.length; // counting number of words
bw.write(nw); // writing to file
bw.write(" "); // entering into new line
}
System.out.println("Total number of words is:"+count); // printing number of words
bw.write("number of words:"+count); // writing number of words to file
}
}