Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me with my java program assignment? I want my code to also perf

ID: 3692377 • Letter: C

Question

Can someone help me with my java program assignment? I want my code to also perform this action (in bold words):

---------------------------------------------------------------------------------------------------------------

Write a program that reads a list of words from a file. You may assume that each line in the file contains one word.

Your program should remove all capitalization, sort the words in alphabetical order, and write the resulting list of words back to another file.

If the input file 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.

-------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------

package readdatafromfile;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class SortFile {
   public static void main(String args[]) throws FileNotFoundException, IOException {
      
       {   
       FileReader fileReader = new FileReader("filer.txt");
       BufferedReader bufferedReader = new BufferedReader(fileReader);
       ArrayList lines = new ArrayList();
       String line = null;
       int numberOfWords = 0;
       while ((line = bufferedReader.readLine()) != null) {
           System.out.println(line);
           lines.add(line);
       }
       bufferedReader.close();
       System.out.println("Read from filer.txt");
           System.out.println("Written to wordsnew.txt");
          
      
           Collections.sort(lines, Collator.getInstance());
      

       FileWriter writer = new FileWriter("wordsnew.txt");
       for(String str: lines) {
       numberOfWords++;
       writer.write(str.toLowerCase() + " ");
       }
       writer.close();
       System.out.println("Number of words: " + numberOfWords);
       }
   }
  

}

Explanation / Answer

For removing duplicate words, you can simply use a Set to store the words. Below you will find my implementation.

package chegg;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class ReadWords {
  
   public static void main(String args[]) throws FileNotFoundException, IOException {
  
   {   
   FileReader fileReader = new FileReader("filer.txt");
   BufferedReader bufferedReader = new BufferedReader(fileReader);
   Set<String> words = new HashSet<String>();
   String line = null;
   int numberOfWords = 0;
   while ((line = bufferedReader.readLine()) != null) {
   System.out.println(line);
   words.add(line);
   }
   bufferedReader.close();
   System.out.println("Read from filer.txt");
   System.out.println("Written to wordsnew.txt");
   FileWriter writer = new FileWriter("wordsnew.txt");
   for(String str: words) {
       numberOfWords++;
       writer.write(str.toLowerCase() + " ");
   }
   writer.close();
   System.out.println("Number of words: " + numberOfWords);
   }
   }

}