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 java program to a

ID: 3692272 • Letter: C

Question

Can someone help me with my java program assignment? I want my java program to also to do this:

If I put one word on each line of the input file, the java program would sort the words in alphabet order and then print the result list of words onto the output file? For example:

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

Input file:

arm

zack

bob

gone

done

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

Output File:

arm

bob

done

gone

zack

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

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

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

package readdatafromfile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;


public class FileRead {
   public static void main(String args[]) throws FileNotFoundException, IOException {
       File f = new File("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("filer.txt")); // object to read the file
      
       String line, nw = "";
       int count = 0; // count to find number of words
       String allLines = "";
       int removeWordsCount = 0;
       int numberOfLines = 0;
       boolean found = false;
       while ((line = br.readLine()) != null) // while for reading till last
       // line of the file
       {
           nw = "";
           numberOfLines++;
           removeWordsCount = 0;
           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);
           if (numberOfLines == 1) {
               nw = arr[0] + " ";
               for (int i = 1; i < arr.length - 1; i++) { // Check if input file
                   found = false; //contains the same word multiple times
                   for (int j = i + 1; j < arr.length; j++) {
                       if (arr[i].equals(arr[j])) {
                           found = true;
                           break;
                       }
                   }
                   if (!found) {
                       nw = nw + arr[i] + " ";
                   }
               }
           } else {
               for (int i = 0; i < arr.length; i++) // loop to append sorted
                                                       // words
               {

                   if (!allLines.contains(arr[i])) {
                       nw = nw + arr[i] + " ";
                   } else {
                       removeWordsCount++;
                   }

               }
           }
          
           allLines = allLines + l + " ";
           count = count + arr.length - removeWordsCount; // counting number of
                                                           // words
           bw.write(nw); // writing to file
           bw.newLine();// entering into new line
       }
       br.close();

       System.out.println("Total number of words is: " + count); // printing
                                                                   // words
       System.out.println("Read from filer.txt");
       System.out.println("Written to Hello.txt");

       bw.write("number of words:" + count); // writing number of words to file
       bw.close();
      
   }
}

Explanation / Answer

I found your code little bit confusing. so, please try the following code. I have done it in my way

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