Created
July 19, 2017 13:13
-
-
Save crissyg/6c35778b0c6bd76614d929284b4551b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Day 8: Dictionaries and Maps | |
/*Description: Hackerrank - 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 queried, | |
print the associated entry from your phone book on a new line in the form name=phoneNumber; | |
if an entry for is not found, print Not found instead.*/ | |
//by crissyg | |
import java.util.*; | |
import java.io.*; | |
class Solution{ | |
public static void main(String []argh){ | |
// Create a Map of String Keys to Integer Values, implemented by the HashMap class | |
Map<String,Integer> myMap = new HashMap<String,Integer>(); | |
Scanner in = new Scanner(System.in); | |
int n = in.nextInt(); | |
for(int i = 0; i < n; i++){ | |
String name = in.next(); | |
int phone = in.nextInt(); | |
myMap.put(name, phone); | |
// Write code here | |
} | |
while(in.hasNext()){ | |
String s = in.next(); | |
// Write code here | |
if(myMap.containsKey(s)== true) | |
{ | |
System.out.println(s + "=" +myMap.get(s)); | |
}else | |
{ | |
System.out.println("Not found"); | |
} | |
} | |
in.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment