-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqb268.java
42 lines (32 loc) · 1.18 KB
/
qb268.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
/* QB-268
Write a program that reads in a list of words from the user and stores them in a HashSet.
The program should then prompt the user for a prefix and output all the words in the set
that start with that prefix.
*/
import java.util.*;
class QB268 {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
Scanner sc = new Scanner(System.in);
// list of words from the user and stores them in a HashSet
while (true) {
System.out.println("Enter Word: ");
String word = sc.nextLine();
set.add(word);
System.out.println("Do You want to Add Continue ? (YES/NO)");
String choice = sc.nextLine();
if (choice.equalsIgnoreCase("No")) {
break;
}
}
// prompt the user for a prefix and output all the words in the set that start
// with that prefix.
System.out.println("Enter Prefix for the word that you want to search");
String prefix = sc.next();
for (String ss : set) {
if (ss.startsWith(prefix)) {
System.out.println("--->" + ss);
}
}
}
}