Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:57:59+00:00 2026-05-28T05:57:59+00:00

I have a chatprogram which contains users and channels. My next quest is to

  • 0

I have a chatprogram which contains users and channels. My next quest is to get a list of channels which one user is in. How should this be done?

Here are the codes as of now:

ChatFrontImpl:

private Hashtable<String, ArrayList<String>> channels = new Hashtable<String, ArrayList<String>>();
private ArrayList<Client> clients;

public synchronized boolean registerClient(Client client, String password) throws RemoteException {
if(!u.logIn(client.findName(), password)){
    System.out.println("Wrong username or password!");
    return false;
}
if (!clients.contains(client)) {
    try {
        clients.add(client);
        updateJlist();
        System.out.println(client.findName() + " registered.");
    } 
    catch (Exception e){
        System.out.println("error in method registerClient(): " + e);
    }
    return true;
}else
    return false;
} 

public void connectChannel(String username, String channel) throws RemoteException{ 
    if(isUserRegistered(username)){
        if (!channels.containsKey(channel)) {
            String message = "User " + username + " entered the channel";
            channels.put(channel, new ArrayList<String>());
            channels.get(channel).add(username);
            notifyChannelSystem(channel, "SYSTEM", message);
            notifySelf(username, "Write /? for avaliable commands");
        }
        else{
            if(channels.get(channel).contains(username)){ 
            } 
            else {
                channels.get(channel).add(username);
                String message = "User " + username + " just entered the channel";
                notifyChannelSystem(channel, "SYSTEM", message);        
          }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:57:59+00:00Added an answer on May 28, 2026 at 5:57 am

    I’d use a different data structure – but assuming that you wish to continue with this one (in order to answer the question):

    public List<String> getChannelsForUsername(String username) {
        List<String> userChannels = new ArrayList<String>();
        for (String channel : channels.keySet()) {
            if (channels.get(channel).contains(username)) {
                userChannels.add(channel);
            }
        }
        return userChannels;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you managed to get Aptana Studio debugging to work? I tried following this,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have 2 threads running a chat program such that one is a server
I have simple chat program using WCF service. One service use for server and
So I just set this up: http://www.phpeasystep.com/phptu/6.html It was fairly easy, now I have
hi I have a scenario like this I made a chat program where a
Have a look at this code: #include <iostream> using namespace std; int main() {
Have an XML with next form: <categories someAttribute=test> <category id=1> <title></title> </category> <category id=1>
Have this method call: -> simpleJdbcTemplate.queryForInt(SQL,null); -> queryForInt() method in the springs SimpleJdbcTemplate throws
Have been scatching my head about this - and I reckon it's simple but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.