942,519 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8364
  • Java RSS
Sep 5th, 2004
0

making a string .. name of an object

Expand Post »
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??
Similar Threads
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Sep 6th, 2004
0

Re: making a string .. name of an object

Here I will create a class named StringClass and build constructors.....

Java Syntax (Toggle Plain Text)
  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"


Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 17
Solved Threads: 8
Junior Poster in Training
Banderson is offline Offline
66 posts
since Aug 2004
Sep 6th, 2004
0

Re: making a string .. name of an object

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.
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Sep 6th, 2004
0

Re: making a string .. name of an object

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.

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 46
Solved Threads: 2
Junior Poster
Iron_Cross is offline Offline
117 posts
since Jul 2003
Sep 8th, 2004
0

Re: making a string .. name of an object

Okay fren ... but I'll keep on trying ...
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Sep 19th, 2004
0

Re: making a string .. name of an object

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
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Tab error
Next Thread in Java Forum Timeline: help creating objects for simulating cd collection





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC