Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Naming Classes based off of String variables?

Posted on 2003-03-18
6
Medium Priority
?
185 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

580 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