Link to home
Start Free TrialLog in
Avatar of Zadel
Zadel

asked on

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!
Avatar of Mick Barry
Mick Barry
Flag of Australia image

why?

> room.add(name);

Which variable is 'room'?


Avatar of Zadel
Zadel

ASKER

'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);
  }
}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ) ;
}
Avatar of Zadel

ASKER

Thanks.  Your solution worked well.
Excellent :)