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.

[4261 byte] By [nick211001a] at [2007-11-27 7:39:01]
# 1
As emails doesn't contain 'staffName' you are clearly going to need another hashtable. Either that or the user has already supplied the staffname and all you have to do is replay it back to him.
ejpa at 2007-7-12 19:19:36 > top of Java-index,Java Essentials,New To Java...
# 2

ejp,

Thanks for the prompt response.

The client inputs the staff name into a field on the client side.

I suppose my question is how do I get method CaseN to understand that I am asking it to get the information in the first "" if it is supplied with the information from the second "".

Case E works but I'm pretty certain that it works due to some default setting

nick211001a at 2007-7-12 19:19:36 > top of Java-index,Java Essentials,New To Java...
# 3

Don't you mean case 'N'?

Case E won't even compile:

Object value = emails.keySet("", "");

keySet() doesn't take any parameters. But as 'staffname' isn't in the 'email' hash map there's no point in using it at all. Where is this information held? that's where you have to look.

ejpa at 2007-7-12 19:19:36 > top of Java-index,Java Essentials,New To Java...
# 4

ejp,

Once again thanks for the prompt reply.

I'm sorry. I not making myself very clear.

I included the Object value line of code to illustrate what I have been trying. I know it doesn;' compile.

I'm trying to figure out the method that distinguishes between say "OU" and "www.open.ac.uk" in the hashtable so that I can create further steps to extract one or the other.

Are you saying that the staffName variable I have given a null value to should have another value. If it does then that brings me back to how to distinguish between the "OU" and "www.open.ac.uk" values.

Your help is greatly appreciated.

nick211001a at 2007-7-12 19:19:36 > top of Java-index,Java Essentials,New To Java...
# 5

> I'm trying to figure out the method that

> distinguishes between say "OU" and "www.open.ac.uk"

> in the hashtable so that I can create further steps

> to extract one or the other.

Neither of them is in the hashtable. I don't get it.

> Are you saying that the staffName variable I have

> given a null value to should have another value. If

> it does then that brings me back to how to

> distinguish between the "OU" and "www.open.ac.uk"

> values.

I don't understand any of this.

A Hashtable is a mapping of keys onto values. It doesn't do anything else. From the above your hash table doesn't contain either "OU" or "www.open.ac.uk" so how can it possibly return either of them?

ejpa at 2007-7-12 19:19:36 > top of Java-index,Java Essentials,New To Java...