Ask Question
3 December, 14:35

Write a simple phonebook program that reads in a series of name-number pairs from the user (that is, name and number on one line separated by whitespace) and stores them in a Map from Strings to Integers. Then ask the user for a name and return the matching number, or tell the user that the name wasn't found.

+1
Answers (1)
  1. 3 December, 17:04
    0
    import java. util. HashMap;

    import java. util. Map;

    import java. util. Scanner;

    public class PhoneBook {

    public static void main (String[] args) {

    Scanner in = new Scanner (System. in);

    Map map = new HashMap ();

    String name, number, choice;

    do {

    System. out. print ("Enter name: ");

    name = in. next ();

    System. out. print ("Enter number: ");

    number = in. next ();

    map. put (name, number);

    System. out. print ("Do you want to try again (y or n) : ");

    choice = in. next ();

    } while (! choice. equalsIgnoreCase ("n"));

    System. out. print ("Enter name to search for: ");

    name = in. next ();

    if (map. containsKey (name)) {

    System. out. println (map. get (name));

    } else {

    System. out. println (name + " is not in the phone book");

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a simple phonebook program that reads in a series of name-number pairs from the user (that is, name and number on one line separated ...” in 📘 Engineering if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers