JAVA PROGRAMMING: Write a program that prompts the user for a word (the trigger
ID: 3674908 • Letter: J
Question
JAVA PROGRAMMING:
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. 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.
The utilities is as below;
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();
}
}
Explanation / Answer
import java.io.FileNotFoundException;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
public class ProcessList {
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);
// list iterator
ListIterator<String> itr=list.listIterator();
String pre = ""; // store current word
String current = ""; // store previous word
while(itr.hasNext()){
int i=0;
i = itr.previousIndex();
pre = current;
current = itr.next();
if(trigger.equalsIgnoreCase(current)){
list.set(i, pre.replaceAll(".", "*")); // replace all character with *
}
}
//printing list
System.out.println(list);
}
}
/*
Output:
Enter file name: sample.txt
Enter trigger word: not
[I, *****, not, *****, not, in, the, rain,
, Not, in, the, ****, Not, on, a, train,
, Not, in, a, ***, Not, in, a, tree,
, I, **, not, like, them, Sam, you, see,
, Not, in, a, *****, Not, in, a, box,
, Not, with, a, *****, Not, with, a, fox,
, I, ****, not, eat, them, here, or, there,
, I, **, not, like, them, anywhere,
]
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.