-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaMap.java
More file actions
63 lines (45 loc) · 1.39 KB
/
Copy pathJavaMap.java
File metadata and controls
63 lines (45 loc) · 1.39 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Question:
Create a phone book using HashMap.
Input:
- Store n people's names and phone numbers.
- Then read names until end of input.
Output:
- If name exists, print: name=phoneNumber
- Otherwise print: Not found
*/
import java.util.*;
import java.io.*;
class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Read number of phone book entries
int n = in.nextInt();
in.nextLine();
// Create HashMap to store name and phone number
HashMap<String, String> map = new HashMap<>();
// Store phone book entries
for (int i = 0; i < n; i++) {
// Read person's name
String name = in.nextLine();
// Read phone number
String phone = in.nextLine();
// Store name and phone number in HashMap
map.put(name, phone);
}
// Process queries until end of input
while (in.hasNext()) {
// Read query name
String name = in.nextLine();
// Check if name exists in phone book
if (map.containsKey(name)) {
// Print name and phone number
System.out.println(name + "=" + map.get(name));
} else {
// Name not found
System.out.println("Not found");
}
}
in.close();
}
}