-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab9_1.java
More file actions
31 lines (28 loc) · 1007 Bytes
/
Lab9_1.java
File metadata and controls
31 lines (28 loc) · 1007 Bytes
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class Lab9_1 {
public static void main(String[] args) {
Hashtable<String, String> phoneBook = new Hashtable<>();
try (BufferedReader br = new BufferedReader(new FileReader("phonebook.txt"))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(" ", 2);
if (parts.length >= 2) {
String name = parts[0];
String phone = parts[1];
phoneBook.put(name, phone);
} else {
System.out.println("ignoring line: " + line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
// Print all phone numbers
for (String name : phoneBook.keySet()) {
System.out.println(name + ": " + phoneBook.get(name));
}
}
}