-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontactBook.java
More file actions
41 lines (36 loc) · 1.36 KB
/
Copy pathcontactBook.java
File metadata and controls
41 lines (36 loc) · 1.36 KB
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
import java.util.Scanner;
public class contactBook {
static String[] contacts = new String[100];
static int count = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("\n1. Add Contact");
System.out.println("2. Search by Prefix");
System.out.println("3. Exit");
System.out.print("Enter choice: ");
int ch = sc.nextInt();
sc.nextLine();
if (ch == 1) {
System.out.print("Enter contact name: ");
contacts[count++] = sc.nextLine().toLowerCase();
} else if (ch == 2) {
System.out.print("Enter prefix: ");
String prefix = sc.nextLine().toLowerCase();
boolean found = false;
for (int i = 0; i < count; i++) {
if (contacts[i].startsWith(prefix)) {
System.out.println("Suggested: " + contacts[i]);
found = true;
}
}
if (!found) System.out.println("No match.");
} else if (ch == 3) {
break;
} else {
System.out.println("Wrong choice.");
}
}
sc.close();
}
}