Results 1 to 5 of 5
  1. #1
    sindrem is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Hashtable question. Getting info from a hashtable

    Yes, i know i should probably use hashmap instead, but at the moment i have a code with hashtable, and i would love to make it work.

    So, i got clients connecting a server, which the clients get added to a list.

    Java Code:
    private ArrayList<Client> clients;
    The clients can connect channels, which they get added to this:

    Java Code:
    private Hashtable<String, ArrayList<String>> channels = new Hashtable<String, ArrayList<String>>();
    And, at the moment im able to get the client list of the server to a JList the gui.

    But now i want to get an array of all the channels one user is in. This is the method i came up with, but it does not work yet.

    Java Code:
      public List<String> getChannelsForUsername(String username) throws RemoteException {
        List<String> userChannels = new ArrayList<String>();
            for (Enumeration e = channels.elements() ; e.hasMoreElements() ;) {
                if (channels.get(channels).contains(username)) {
                    userChannels.add(channels);
                }
            }
        return userChannels;
        }
    I'm not allowed to add (channels), and the errormessage is: No suitable method found for add(java.util.Hashtable<java.lang.String.java.util .ArrayList<java.lang.String>.

    But i am allowed to put username instead of channels. So, whats wrong?

    Im really stuck!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: Hashtable question. Getting info from a hashtable

    Your ArrayList is for Strings (as you defined it).
    So it can't take a Hashtable of stuff.
    I'm surprised the if statement before it didn't give an error, as I would have thought get() would have expected a String for the key as well.

  3. #3
    sindrem is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Hashtable question. Getting info from a hashtable

    Oh, thats right. Do u have any idea on how to make it work?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: Hashtable question. Getting info from a hashtable

    I have no idea since I'm not terribly clear on what exactly it is you're trying to do.

  5. #5
    sindrem is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Hashtable question. Getting info from a hashtable

    Ill try to explain. Its much like IRC, with channels. Every channel has users in it. Right now im able to chat in channels, switch channels, leave channels, but i want a Jlist showing all the channels which the current client is connected to. So, in under to get that JList showing the current channels, i need this method to work :)

    I'm also going to make a method which show all users in a marked channel, with a JList. At the moment i already have a JList showing ALL clients online, but that was way easier, since its just an arraylist keeping the connected clients.

Similar Threads

  1. hashtable
    By kmm1977 in forum New To Java
    Replies: 4
    Last Post: 01-20-2011, 04:24 PM
  2. hashtable
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-06-2009, 08:20 AM
  3. hashtable
    By vijayabaskar in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 08:05 AM
  4. Hashtable
    By angelicsign in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 04:30 PM
  5. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •