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.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class PatternMatcher {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
String fileName;
ArrayList wordList;
System.out.print("Enter a file name to search in: ");
fileName = scnr.nextLine();
try {
wordList = getWordList(fileName);
System.out.println("File read successfully, initiating pattern matcher...");
} catch (FileNotFoundException e){
System.out.println("Error: File not found, exiting...");
scnr.close();
return;
}
while (true){
System.out.print("Enter a pattern to match (or press Enter to exit): ");
String pattern = scnr.nextLine().trim();
if (pattern.isEmpty()){
System.out.println("Exiting...");
break;
}
ArrayList matches = getMatches(wordList, pattern);
System.out.println("Matches for pattern \""+ pattern +"\": "+ matches);
}
scnr.close();
}
public static ArrayList getWordList(String filename) throws FileNotFoundException {
ArrayList wordList = new ArrayList<>();
FileInputStream file = new FileInputStream(filename);
Scanner fileScnr = new Scanner(file);
while (fileScnr.hasNext()){
String newWord = fileScnr.next().trim();
if (!newWord.isEmpty()){
wordList.add(newWord);
}
}
fileScnr.close();
return wordList;
}
public static boolean isMatchAtIndex(String word, String pattern, int index){
if (index <0|| index >= word.length()|| index >= pattern.length()){
return false;
}
char patternChar = pattern.charAt(index);
char wordChar = word.charAt(index);
return patternChar =='_'|| patternChar == wordChar;
}
public static boolean isMatch(String word, String pattern){
if (word.length()!= pattern.length()){
return false;
}
for (int i =0; i < word.length(); i++){
if (!isMatchAtIndex(word, pattern, i)){
return false;
}
}
return true;
}
public static ArrayList getMatches(ArrayList wordList, String pattern){
ArrayList matches = new ArrayList<>();
for (String word : wordList){
if (isMatch(word, pattern)){
matches.add(word);
}
}
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
m_th__
_x_y_z
"
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 "m_th__": [methyl]
Enter a pattern to match (or press Enter to exit): Matches for pattern "_x_y_z": []
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
m_th__
_x_y_z
"
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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!