Ask Question
6 September, 10:15

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.

+4
Answers (1)
  1. 6 September, 12:13
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers