HashTable
Hi,
I have been through most of the hashtable tutorials and can't find the answer I'm looking for.
How do I set up a hashtable (must be a hashtable) and then create a method that extracts either the name or the email address dependent on the clientLine input?
I have the following code extract:
publicclass Server{
private PrintWriter pw;
private BufferedReader bf;
private ServerSocket ss;
private Socket s;
//Hashtable used to store test data
//Set up hash table with test data
private Hashtable emails;
public Server()throws Exception{
emails =new Hashtable();
emails.put("Ince","ince@hotmail.com");
emails.put("Roberts","roberts@hotmail.com");
emails.put("Timms","timms2@yahoo.com");
emails.put("Rowlands","Rowlands@tiny.com");
emails.put("Eustace","Eustace@yahoo.com");
emails.put("Lord","Lord3@hotmail.com");
System.out.println("...Setting up server socket");
ss =new ServerSocket(1200);
System.out.println("..waiting for connection ");
s = ss.accept();
System.out.println("..connection made");
InputStream is = s.getInputStream();
bf =new BufferedReader(new InputStreamReader(is));
OutputStream os = s.getOutputStream();
pw =new PrintWriter(os,true);
}
publicvoid Run()throws Exception
{
boolean cont =true;
while(cont ==true)//server runs while true
{
String clientLine = bf.readLine();
System.out.println( clientLine );//used to check the connection
switch(clientLine.charAt(0))
{
case'E':
{
String Name ="";
String staffName =null;
Object value = emails.keySet("","");
Name = clientLine.substring(1,clientLine.length());
staffName = (String) emails.get(Name);
pw.println(staffName);
break;
}
case'N':
{
String email ="";
String emailAddress =null;
email = clientLine.substring(1,clientLine.length());
emailAddress = (String) emails.get(email);
pw.println(emailAddress);
break;
}
For the CaseE, wIthout the Object line of code I am able to extract the emailAddress by inputting the name. I need to do the opposite for CaseN.
As you can see I have been trying to implement keySet with no success. Any help on the correct syntax would be very welcome.

