Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The grepScanner method should read one line at a time from the Scanner object an

ID: 3827902 • Letter: T

Question

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.

public static ArrayList<?1?> grepScanner(Scanner scanner, String pattern) {
ArrayList<?1?> list = new ArrayList<?1?>();
while (?2?.?3?()) {
String line = ?4?.?5?();
if (?6?.?7?(?8?) >= 0) {
list.?9?(?10?);
}
}
scanner.close();
return list;
}

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.

public static ArrayList<?11?> grepFile(File file, String pattern) {
?12? {
return grepScanner(new ?13?(?14?), ?15?);
} ?16? (?17? exception) {
return ?18? ?19?<?11?>();
} ?16? (?20? exception) {
return null;
}
}

1 What should replace ?1? ?

2 What should replace ?2? ?

3 What should replace ?3? ?

4 What should replace ?4? ?

5 What should replace ?5? ?

6 What should replace ?6? ?

7 What should replace ?7? ?

8 What should replace ?8? ?

9 What should replace ?9? ?

10 What should replace ?10? ?

11 What should replace ?11? ?

12 What should replace ?12? ?

13 What should replace ?13? ?

14 What should replace ?14? ?

15 What should replace ?15? ?

16 What should replace ?16? ?

17 What should replace ?17? ?

18 What should replace ?18? ?

19 What should replace ?19? ?

20 What should replace ?20? ?

Explanation / Answer

Hi, Please find my answer.

public static ArrayList<String> grepScanner(Scanner scanner, String pattern) {
ArrayList<String> list = new ArrayList<String>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.indexOf(pattern) >= 0) {
list.add(line);
}
}
scanner.close();
return list;
}


public static ArrayList<?11?> grepFile(File file, String pattern) {
try(file != null && pattern != null) {
return grepScanner(new Scanner(file), pattern);
} catch (FileNotFoundException exception) {
return new ArrayList<String>();
} catch (Exception exception) {
return null;
}
}

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote