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

Need help with java program assignment. I need help with this part of the java p

ID: 3692637 • Letter: N

Question

Need help with java program assignment. I need help with this part of the java program assignment (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.

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

I would be really appreciate if someone can help me with this assignment.

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

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

Hello there ,

Expalination :

For having unique set of word you should use "Set/HashSet" -> contains unique element.

Now you want them in sorted manner , you should use TreeSet .-> Contaions unique element in sorted order.

While inserting into tree set it sorts the elemets.

So we do not need to explicitly sort.

I hope you are clear.

Please find below code.

And yes let me know if you have any queries.

Thanks.

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.Collections;
import java.util.Set;
import java.util.TreeSet;

public class Assignment1 {
   public static void main(String args[]) throws FileNotFoundException, IOException {

       {
           FileReader fileReader = new FileReader("filer.txt");
           BufferedReader bufferedReader = new BufferedReader(fileReader);
           Set<String> setOfSortedWord = new TreeSet<String>(); // Set to
                                                                   // obtain
                                                                   // unique
                                                                   // words and
                                                                   // in sorted
                                                                   // manner as
                                                                   // it is
                                                                   // tree set.
           String line = null;
           System.out.println("Reading from filer.txt");

           while ((line = bufferedReader.readLine()) != null) {
               System.out.println(line.trim());
               setOfSortedWord.add(line.trim().toLowerCase()); // Adding word in set
                                                           // so that we can
                                                           // have unique
                                                           // values.
           }
           bufferedReader.close();
           System.out.println("Writing to wordsnew.txt");

           FileWriter writer = new FileWriter("wordsnew.txt");
           for (String word : setOfSortedWord) {
               System.out.println(word);
               writer.write(word + " ");
           }
           writer.close();
           System.out.println("Number of words: " + setOfSortedWord.size());
       }
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote