Naming Classes based off of String variables?

I would like to create instances of a class that are named according to the value of a String variable (which is determined by the user).  How is this possible?


  public void newRoom(String name){
    try{
    Room /*whatever name is*/ = new Room();
    num_rooms++;
    room.add(name);
    }
    catch(Exception e){
      System.err.println(e);
    }
  }



Thanks!
ZadelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
why?

> room.add(name);

Which variable is 'room'?


0
ZadelAuthor Commented:
'room' is a vector and 'Room' is a class.  (Okay, perhaps I should rename these to reduce potential confusion.)  I am wanting to create specfic "Room" classes that are named according to what the user enters as room-names.  Perhaps this is not the best way of accomplishing what is needed in this program.  Even so, I would like to know what to do such a thing in Java (if it is indeed possible).

Here is a longer portion of the code.  This includes both the RoomManager Class and the (beginnings of the) Room Class.  If anything needs clarification, please ask.

Thanks again.


class RoomManager{
  Vector room = new Vector();
  int num_rooms=0;

  RoomManager(){
    int num_rooms=0;
  }

  public void newRoom(String name){
    try{
    Room /*whatever "name" is*/ = new Room();
    num_rooms++;
    room.add(name);
    }
    catch(Exception e){
      System.err.println(e);
    }
  }

  public int getRoomNum(){
    return num_rooms;
  }

  public String getRmName(int i){
    try{
      return room.get(i).toString();
    }
    catch(NullPointerException e){
      return "No rooms.";
    }
  }

  public void processRmName(String name){
    try{
      System.out.println("Processing neme ... " + name);
      int i=0;
      for(i=0;i<num_rooms;i++){
        if(name.equals(getRmName(i))){
          //add user to room
          break;
        }
      }

      if (i==num_rooms){
        //add new room
        newRoom(name);
        //and put user in it
      }

      else
        System.out.println("Check this");

      System.out.println("num rooms: " + getRoomNum());

     }
    catch(NullPointerException e){
      System.err.println(e);
    }
  }
}


class Room{
  //The members are IDed by their socket information
  Vector members = new Vector();

  int numMembers=0;

  void addMember(Socket sock){
    numMembers++;
    members.add(sock);
  }
}
0
objectsCommented:
Just store the room name in your Room class:

class Room{
 //The members are IDed by their socket information
 Vector members = new Vector();
 String name = null;
 int numMembers=0;

 public Room(String name)
 {
   this.name = name;
 }

 public String getName()
 {
   return name;
 }

 void addMember(Socket sock){
   numMembers++;
   members.add(sock);
 }
}


........................


public void newRoom(String name){
   try{
   Room r = new Room(name);
   num_rooms++;
   room.add(r);
   }
   catch(Exception e){
     System.err.println(e);
   }
 }

 public String getRmName(int i){
   try{
     return ((Room)room.get(i)).getName();
   }
   catch(NullPointerException e){
     return "No rooms.";
   }
 }

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

TimYatesCommented:
Or, have a hashtable, and add the rooms in with the name as the key...

--------------------------------------------------

Hashtable roomHash = new Hashtable() ;

public void newRoom(String name)
{
  try
  {
    Room rm = new Room();
    if( roomHash.get( name ) != null )
      System.err.println( "Warning, overwriting a room called " + name ) ;
    roomHash.put( name, rm ) ;
  }
  catch(Exception e)
  {
    System.err.println(e);
  }
}

public int countRooms()
{
  return roomHash.size() ;
}

public Room getRoom( String name )
{
  return (Room)roomHash.get( name ) ;
}
0
ZadelAuthor Commented:
Thanks.  Your solution worked well.
0
objectsCommented:
Excellent :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.