?
Solved

Naming Classes based off of String variables?

Posted on 2003-03-18
6
Medium Priority
?
177 Views
Last Modified: 2010-03-31
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!
0
Comment
Question by:Zadel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8164863
why?

> room.add(name);

Which variable is 'room'?


0
 

Author Comment

by:Zadel
ID: 8164942
'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
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 8164974
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 8165406
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
 

Author Comment

by:Zadel
ID: 8170698
Thanks.  Your solution worked well.
0
 
LVL 92

Expert Comment

by:objects
ID: 8170727
Excellent :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question