-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStage2.java
52 lines (50 loc) · 1.94 KB
/
Stage2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package search;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* A program that reads text lines from the standard input
* and processes single-word queries.
* The program outputs all lines that contain the string
* from the query.
* For this stage, this includes the case
* where the query string appears as a substring of one of the text lines.
* For example, the query "bc" should be found in a line containing "abcd".
*/
public class Stage2 {
private static List<String> getWordsThatMatch(
final String toSearch, final ArrayList<String> listOfPerson) {
List<String> matchingLines = new ArrayList<>();
for (String person : listOfPerson) {
if (person.toLowerCase().contains(toSearch)) {
matchingLines.add(person);
}
}
return matchingLines;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of people:");
int noOfPerson = Integer.parseInt(scanner.nextLine());
ArrayList<String> listOfPerson = new ArrayList<>();
System.out.println("Enter all people:");
while (noOfPerson-- > 0) {
listOfPerson.add(scanner.nextLine());
}
System.out.println("Enter the number of search queries:");
int queries = Integer.parseInt(scanner.nextLine());
while (queries-- > 0) {
System.out.println("Enter data to search people:");
String wordToSearch = scanner.nextLine();
List<String> find =
getWordsThatMatch(wordToSearch.toLowerCase(), listOfPerson);
if (find.isEmpty()) {
System.out.println("No matching people found.");
}
else {
System.out.println("Found people:");
find.forEach(System.out::println);
}
}
}
}