Skip to content

Instantly share code, notes, and snippets.

@abdulateef
Created June 20, 2016 07:55
Show Gist options
  • Save abdulateef/94c69ebcaec8d314487e1a7800127a66 to your computer and use it in GitHub Desktop.
Save abdulateef/94c69ebcaec8d314487e1a7800127a66 to your computer and use it in GitHub Desktop.
Given names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for; for each name queried, print the associated entry from your phone book (in the form name=phonenumber ) or NOT FOUND if there is no entry for .name
//Day8
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh)
{
Map<String,Long> phonebook = new java.util.HashMap<>();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i = 0; i < n; i++)
{
String name = in.next();// to get names i.e keys
long phone = in.nextLong();// to get numbers i.e values
phonebook.put(name, phone);
//System.out.println(phonebook);
}
while(in.hasNext())
{
String name = in.next(); // to enter key to fetch a specific value
if(phonebook.containsKey(name)== true)
{
System.out.println(name + "=" +phonebook.get(name));
}else
{
System.out.println("Not found");
}
}
in.close();
}
}
@ajinkya933
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment