Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.2 kB
2
Indexable
Never
package JavaCollection;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class PhoneBook {
    public static void main(String[] args) {
        System.out.println("How many entries to be made in phone book?");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        System.out.println("Enter the person name and phone number");
        HashMap<String, String> phoneBookMap = new HashMap<String, String>();
        for (int i = 0; i < n; i++) {
            String name = scanner.next();
            String number = scanner.next();
            phoneBookMap.put(name, number);
        }
        System.out.println(phoneBookMap);
        System.out.println("Enter the person name to be searched:");
        String person = scanner.next();

        for (Map.Entry<String, String> pair : phoneBookMap.entrySet()) {
            if (phoneBookMap.containsKey(person)) {
                System.out.println(pair.getKey() + "=" + pair.getValue());
                break;
            } else {
                System.out.println("Sorry,No entry made in phonebook");
            }

        }
    }
}
Leave a Comment