The Artima Developer Community
Sponsored Link

Java Answers Forum
who can do this?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
nicky

Posts: 1
Nickname: nicky
Registered: Jan, 2003

who can do this? Posted: Jan 22, 2003 8:59 AM
Reply to this message Reply
Advertisement
Hi
The code below prints this out
[goods out, firstname, lastname]
[id, adress1]
[country, phone]
[goods in]
Now how can i get my code to print out the same, but instead of using the strings Values and keys,
use hashtables?
Also how can i do it without using the (public static void getval)
Hope You can solve this
nicky

import java.io.*;
import java.lang.*;
import java.util.*;
public class has3{
public static void main(String[] args) {
String keys[] = {
"firstname", "lastname", "id", "adress1", "phone",
"county", "post code", "country", "goods in", "goods out"
};//System.out.println(keys);
String values[] = {
"ben", "ben", "alex", "alex", "matt",
"matt", "mum", "mum", "dad", "ben"
};
// First, map keys to values in a hashtable
Hashtable hash = new Hashtable();
// the code matches values one by one for each of the keys
for(int i = 0; i < keys.length; i++)
{
hash.put(keys[i], values[i]);

}
// Then we find each value and remove it from the hashtable
for(int j = 0; j < values.length; j++)

{
getval(values[j],hash);

// This will also print it to the screen


}


}
private static void getval(Object value, Hashtable ht ){


ArrayList arr = new ArrayList();

if( ht.containsValue( value ))
{
Enumeration e = ht.keys();
while (e.hasMoreElements())
{
String tempkey = (String)e.nextElement();
String tempvalue = (String)ht.get(tempkey);
if (tempvalue.equals(value))
{
arr.add(tempkey);
ht.remove(tempkey);
}
}System.out.println(arr);
}
}

}

Topic: Preloaded Servlet Previous Topic   Next Topic Topic: WindError.java solution(s)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use