This quiz asks you to complete the two methods described below. Each item to be
ID: 3770019 • Letter: T
Question
This quiz asks you to complete the two methods described below. Each item to be filled in is marked by ?n?. Question n asks you to fill in ?n?. The grepScanner method should read one line at a time from the Scanner object and return all the lines that contain the pattern as a substring. The grepFile method should call the above grepScanner method using a Scanner created from the File object. However, two runtime errors should be handled. If the file is not found, then this method should return an empty ArrayList. If file and/or pattern is null, then this method should return null. This method should not handle any other runtime errors.Explanation / Answer
I have written the code for the second grepfile function in java using regex from library. Code is :
public static ArrayList grepFile(File file,String pattern)
{
Matcher matcher = pattern.matcher("");
try
{
input_read = new BufferedReader(new FileReader(file));
}
catch (IOException e)
{
System.err.println("Unable to read the line '" + file + "': " + e.getMessage());
}
while ((line = input_read.readLine()) != null)
{
matcher.reset(line);
if (matcher.find())
{
System.out.println(file + " contains this : " + line);
}
}
input_read.close();
}
I am handing the exception with the try catch block as mentioned in the problem statement.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.