Hash Table Extract
Hi,
I'm hoping that someone will be able to explain how I extract the first element (in this case the name) from the hashtable in the server code below (case N:).
import java.net.*;
import java.io.*;
import java.util.*;
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;
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;
}
case'U':
{
String number = (String) emails.get(clientLine);
if(number ==null)
pw.println(number);
break;
}
case'Q':
pw.close();
bf.close();
s.close();
cont =false;
break;
}
}
}
publicstaticvoid main(String[]args)throws Exception{
{
Server serv =new Server();
serv.Run();
}
}
}

