JAVA PROGRAMMING Modify Listing 219 CountOccurrenceOfWordsja

JAVA PROGRAMMING

Modify Listing 21.9 (CountOccurrenceOfWords.java). Your program should take a text file name as a command line argument. It should read the file and print the number of times the most-used word appears and, in ascending order, the words that occur most frequently. Ignore case when processing the words in the file. Your program may ignore punctuation. For example, if the file contained

The program\'s output should be

Words that appear 2 times: and are dachshunds dogs

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

utilities.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Utilities {

   public static final String DEFAULT_FILE = \"sample.txt\";

   public static List<String> readAFile() throws FileNotFoundException {
       return readAFile(DEFAULT_FILE);
   }

   public static List<String> readAFile(String fileName) throws FileNotFoundException {
       ArrayList<String> content = new ArrayList<>();

       File f = new File(fileName);
       Scanner scan = new Scanner(f);

       while (scan.hasNextLine()) {
           String l = scan.nextLine();
           Scanner lineBust = new Scanner(l);
           while (lineBust.hasNext()) {
               content.add(lineBust.next());
           }
           lineBust.close();
           content.add(\"\ \");
       }
       scan.close();

       return content;
   }

   public static void printAList(List<String> list) {
       System.out.println();
       for (String s: list) {
           System.out.print(s + \" \");
       }
       System.out.println();
   }

}

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

CountOccurrenceOfWords.java

import java.io.FileNotFoundException;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;

public class plist {

public static void main(String[] args) throws FileNotFoundException
{
Scanner sc = new Scanner(System.in);

String fileName;
String trigger;

System.out.print(\"Enter file name: \");
fileName = sc.next();

System.out.print(\"Enter trigger word: \");
trigger = sc.next();

List<String> list = Utilities.readAFile(fileName);

ListIterator<String> itr=list.listIterator();
  
String pre = \"\";
String current = \"\";
  
while(itr.hasNext())
{
int i=0;
  
i = itr.previousIndex();
pre = current;
current = itr.next();

if(trigger.equalsIgnoreCase(current))
{
  
list.set(i, pre.replaceAll(\".\", \"*\"));
}
}
  
System.out.println(list);   
}
}

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.

OUTPUT:

Words that appear 2 times:

and are dachshunds dogs

JAVA PROGRAMMING Modify Listing 21.9 (CountOccurrenceOfWords.java). Your program should take a text file name as a command line argument. It should read the fil
JAVA PROGRAMMING Modify Listing 21.9 (CountOccurrenceOfWords.java). Your program should take a text file name as a command line argument. It should read the fil

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site