Question: import static org.junit.Assert.fail; import java.io . FileInputStream; import java.io . FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class PatternMatcher { public static void main ( String
import static org.junit.Assert.fail;
import java.ioFileInputStream;
import java.ioFileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class PatternMatcher
public static void mainString args
Scanner scnr new ScannerSystemin;
String fileName;
ArrayList wordList;
System.out.printEnter a file name to search in: ;
fileName scnrnextLine;
try
wordList getWordListfileName;
System.out.printlnFile read successfully, initiating pattern matcher...";
catch FileNotFoundException e
System.out.printlnError: File not found, exiting...";
scnrclose;
return;
while true
System.out.printEnter a pattern to match or press Enter to exit: ;
String pattern scnrnextLinetrim;
if patternisEmpty
System.out.printlnExiting;
break;
ArrayList matches getMatcheswordList pattern;
System.out.printlnMatches for pattern pattern : matches;
scnrclose;
public static ArrayList getWordListString filename throws FileNotFoundException
ArrayList wordList new ArrayList;
FileInputStream file new FileInputStreamfilename;
Scanner fileScnr new Scannerfile;
while fileScnrhasNext
String newWord fileScnr.nexttrim;
if newWord.isEmpty
wordList.addnewWord;
fileScnr.close;
return wordList;
public static boolean isMatchAtIndexString word, String pattern, int index
if index index word.length index pattern.length
return false;
char patternChar pattern.charAtindex;
char wordChar word.charAtindex;
return patternChar patternChar wordChar;
public static boolean isMatchString word, String pattern
if wordlength pattern.length
return false;
for int i ; i word.length; i
if isMatchAtIndexword pattern, i
return false;
return true;
public static ArrayList getMatchesArrayList wordList, String pattern
ArrayList matches new ArrayList;
for String word : wordList
if isMatchword pattern
matches.addword;
return matches;
FAIL Program did not print out correct list of words to match pattern.
given a wordList taken from words.txt
and the input pattern : "words.txt
tto
mth
xyz
Expected :
Enter a file name to search in : File read successfully, initiating pattern matcher...
Enter a pattern to match or press Enter to exit: motto petto, sotto, tutto
Enter a pattern to match or press Enter to exit: methyl
Enter a pattern to match or press Enter to exit:
Enter a pattern to match or press Enter to exit: Exiting...
but got :
Enter a file name to search in: File read successfully, initiating pattern matcher...
Enter a pattern to match or press Enter to exit: Matches for pattern tto": motto petto, sotto, tutto
Enter a pattern to match or press Enter to exit: Matches for pattern mth: methyl
Enter a pattern to match or press Enter to exit: Matches for pattern xyz:
Enter a pattern to match or press Enter to exit: Exiting...
Test your code manually to identify and correct any errors.
FAIL Program did not print out correct list of words to match pattern.
given a wordList taken from words.txt
and the input pattern : "nonexistant.txt
tto
mth
xyz
Expected :
Enter a file name to search in : Error: File not found, exiting...
but got :
Enter a file name to search in: Error: File not found, exiting...
Test your code manually to identify and correct any errors.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
