Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please include detailed comments and keep the code as simple as possible so that I may understand. Thanks The purpose of this exercise is to

Please include detailed comments and keep the code as simple as possible so that I may understand. Thanks

The purpose of this exercise is to use a ListIterator to scan and edit a document.

Write a program that prompts the user for a word (the trigger) and name of a text file.

The program should use the attached Utilities class to read the text file into a List. It should then process it using only a ListIterator. Processing should start at the beginning of the list and proceed to the end. Whenever the trigger word is encountered (use ignoreCase comparisons), the previous word in the document should be changed to all '*' characters. Line breaks should be maintained and do not affect the modification of the previous word. After the list is processed, use the Utilities class to print it. For example, if the trigger word was not, and the file contained

I would not could not in the rain Not in the dark Not on a train Not in a car Not in a tree I do not like them Sam you see Not in a house Not in a box Not with a mouse Not with a fox I will not eat them here or there I do not like them anywhere

The output would be I ***** not ***** not in the **** Not in the **** Not on a ***** Not in a *** Not in a tree I ** not like them Sam you *** Not in a ***** Not in a *** Not with a ***** Not with a fox I **** not eat them here or there I ** not like them anywhere

Do not use List's get method or any other means of accessing or manipulating the list except for the ListIterator. You may assume the file contains no punctuation.

Hint: there is no sophisticated, slick tool that will do this processing. It's a brute force (ugly) program that requires the code to keep up with where the iterator is pointing, what has to be done with the iterator to find the target word, and what has to be done to reposition the iterator after zapping the target.

Utilities:

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 readAFile() throws FileNotFoundException { return readAFile(DEFAULT_FILE); }

public static List readAFile(String fileName) throws FileNotFoundException { ArrayList 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 list) { System.out.println(); for (String s: list) { System.out.print(s + " "); } System.out.println(); }

}

Sample Text:

I would not could not in the rain Not in the dark Not on a train Not in a car Not in a tree I do not like them Sam you see Not in a house Not in a box Not with a mouse Not with a fox I will not eat them here or there I do not like them anywhere

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

3. Identify challenges to good listening and their remedies

Answered: 1 week ago

Question

4. Identify ethical factors in the listening process

Answered: 1 week ago