Q.Create a hashtable containing city name & STD code. Display the details of the hashtable. Also search for a specific city and display STD code of that city.[25 M]
import java.util.*;
class HashtableDemo
{
public static void main(String args[])
{
// Create Hashtable
Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
// Add city and STD code
ht.put("Pune", 020);
ht.put("Mumbai", 022);
ht.put("Delhi", 011);
ht.put("Chennai", 044);
ht.put("Nagpur", 0712);
// Display Hashtable
System.out.println("City and STD Codes:");
Enumeration<String> e = ht.keys();
while(e.hasMoreElements())
{
String city = e.nextElement();
System.out.println(city + " : " + ht.get(city));
}
// Search specific city
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter city to search: ");
String searchCity = sc.next();
if(ht.containsKey(searchCity))
{
System.out.println("STD Code of " + searchCity + " is " + ht.get(searchCity));
}
else
{
System.out.println("City not found in Hashtable");
}
}
}