making a string .. name of an object

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2004
Posts: 1,749
Reputation: nanosani is an unknown quantity at this point 
Solved Threads: 54
Team Colleague
nanosani's Avatar
nanosani nanosani is offline Offline
Unauthenticated Liar

making a string .. name of an object

 
0
  #1
Sep 5th, 2004
I want to input a string
Suppose the string was :
String s = "sani";

Now I want to make "sani" the name of an object of one of the class.
Suppose the name of the class is myClass.
I want to :

myClass sani = new myClass();

Any ideas??
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 47
Reputation: Banderson is an unknown quantity at this point 
Solved Threads: 4
Banderson's Avatar
Banderson Banderson is offline Offline
Light Poster

Re: making a string .. name of an object

 
0
  #2
Sep 6th, 2004
Here I will create a class named StringClass and build constructors.....

  1. public class StringClass
  2. {
  3.  
  4. public static String StringAsObject;
  5.  
  6. // Defining the new Value of s when calling this method
  7. StringClass()
  8. {
  9. StringAsObject = "sani";
  10. }
  11.  
  12.  
  13. // Building Constructors
  14.  
  15. public static String getStringAsObject()
  16.  
  17. {
  18. return StringAsObject;
  19. }
  20.  
  21.  
  22. public void setStringAsObject(String s)
  23. {
  24. StringAsObject = s;
  25. }
  26.  
  27. }

Now I will create a class named myClass that also contains method main and call the value of s which is the string "sani"


  1.  
  2. public class myClass
  3. {
  4. public static void main(String [] args)
  5. {
  6. StringClass aString = new StringClass();
  7. String s;
  8. s = aString.getStringAsObject();
  9.  
  10. StringClass.getStringAsObject();
  11. System.out.println(s);
  12.  
  13.  
  14. }
  15. }

If this is not what you were referring to let me know because I am unclear.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 1,749
Reputation: nanosani is an unknown quantity at this point 
Solved Threads: 54
Team Colleague
nanosani's Avatar
nanosani nanosani is offline Offline
Unauthenticated Liar

Re: making a string .. name of an object

 
0
  #3
Sep 6th, 2004
Thanks Branderson

StringClass aString = new StringClass();

this is what you wrote but I want it as

StringClass sani = new StringClass();

Is it possible like that .... whenever I input a string it should become the name of an object.
Reply With Quote Quick reply to this message  
Join Date: Jul 2003
Posts: 117
Reputation: Iron_Cross is an unknown quantity at this point 
Solved Threads: 2
Iron_Cross's Avatar
Iron_Cross Iron_Cross is offline Offline
Junior Poster

Re: making a string .. name of an object

 
0
  #4
Sep 6th, 2004
No it's not. The closest thing you'll be able to do is to use a HashMap. Which is a list of key/value pairs. The string would be the key, and the object would be the value.

  1. public class HashTest {
  2. private HashMap objList;
  3. private Object obj;
  4. private String objName;
  5.  
  6. public HashTest(){
  7. this(20);
  8. }
  9.  
  10. public HashTest(int size){
  11. this.objList = new HashMap(size);
  12. }
  13.  
  14. public void add(Object obj, String name){
  15. this.objList.put(obj,name);
  16. }
  17.  
  18. public void remove(String name){
  19. this.objList.remove(name);
  20. }
  21.  
  22. public Object get(String name){
  23. if(this.objList.containsKey(name))
  24. this.objList.get(name);
  25. else
  26. return;
  27. }
  28.  
  29. public void showObjects(){
  30. String name;
  31. ArrayList ar = new ArrayList(this.objList.values());
  32. for(int i = 0; i < ar.size(); i++){
  33. name = ar.get(i);
  34. System.out.println(name);
  35. }
  36. }
  37.  
  38. public static void main(String[] args){
  39. HashTest test = new HashTest(5);
  40.  
  41. String name = "sani";
  42. Object o = new Object(); // <-- that would be some object you want to name
  43. String name2 = "mandy";
  44. Object o2 = new Object();
  45.  
  46. test.add(o,name);
  47. test.add(o2,name2);
  48.  
  49. test.showObjects();
  50. test.remove("sani");
  51.  
  52. test.showObjects();
  53. }
  54. }
Try that out, you may need to add some casts and stuff, but you should get the idea.
elitehackers.info
Today's Penny-Arcade!
Pain is weakness leaving the body!
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 1,749
Reputation: nanosani is an unknown quantity at this point 
Solved Threads: 54
Team Colleague
nanosani's Avatar
nanosani nanosani is offline Offline
Unauthenticated Liar

Re: making a string .. name of an object

 
0
  #5
Sep 8th, 2004
Okay fren ... but I'll keep on trying ...
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: making a string .. name of an object

 
0
  #6
Sep 19th, 2004
hi everyone,
Is this what you mean?

String a = "sani";
Object k;

k = (Object)a;
//k has already been assigned the string "sani"
myClass k = new myClass( );

I hope this helps you

Yours Sincerely

Richard West
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC