Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java: Getting object reference from string

Options
  • 29-04-2006 9:16pm
    #1
    Registered Users Posts: 3,357 ✭✭✭


    I know this is probably very simple.

    I have an object, let's pretend it's of type Person. I have a method .getAge() for type Person.

    Someone types in a person's name. I need make this string entered become a reference to a Person object (that already exists) that (purely by naming convention) will have a reference to it already in place that is called the same thing as what the user would be entering, so I can apply the method to the object.

    I want to get a reference to the object from a string with the name of the object.

    Am I being too vague?

    Please help!


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Use a Hashmap/Hashtable to store your collection of Person objects?


  • Registered Users Posts: 3,357 ✭✭✭snappieT


    Appreciate the idea, but kinda too late to implement, also very much Java beginner, not too confident on doing that.

    Is there no way to get an object from a string?


  • Registered Users Posts: 2,800 ✭✭✭voxpop


    How are you storing your collection of Person objects?

    basically loop through the collection and check the name entered with the name stored in the person object

    for each person P
    if (enteredName.equals(P.getName())
    do stuff


  • Registered Users Posts: 4,188 ✭✭✭pH


    Not too difficult to use hashmaps ...
    HashMap people = new HashMap();
    Person peter = new Person();
    Person karen = new Person();
    
    // Storing people
    
    people.put("Peter", peter);
    people.put("Karen", karen);
    
    // Retrieving
    
    Person retrieve = (Person)people.get("Peter");
    
    


  • Registered Users Posts: 2,835 ✭✭✭StickyMcGinty


    pH wrote:
    Not too difficult to use hashmaps ...
    HashMap people = new HashMap();
    Person peter = new Person();
    Person karen = new Person();
    
    // Storing people
    
    people.put("Peter", peter);
    people.put("Karen", karen);
    
    // Retrieving
    
    Person retrieve = (Person)people.get("Peter");
    
    

    yea hashmaps are the way to go for this, but if you think its too much to do in a small amount of time, use voxpops way! just keep checking for the person.name to be equal to the string your searching for


  • Advertisement
  • Registered Users Posts: 3,357 ✭✭✭snappieT


    voxpop wrote:
    How are you storing your collection of Person objects?

    basically loop through the collection and check the name entered with the name stored in the person object

    for each person P
    if (enteredName.equals(P.getName())
    do stuff
    Good idea. Unfortunately, it's not actually a Person object, it's a JLabel. And as far as I know, there isn't a method to get the name of the JLabel object.

    I think my only option is
    if (stringentered == "01") activelabel = p01;
    if (stringentered == "02") activelabel = p02;
    if (stringentered == "03") activelabel = p03;
    .
    .
    .
    if (stringentered == "n") activelabel = pn;
    


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    I have an object, let's pretend it's of type Person. I have a method .getAge() for type Person.
    Unfortunately, it's not actually a Person object, it's a JLabel.

    I think we need to go back to the start here...


  • Registered Users Posts: 4,188 ✭✭✭pH


    t's a JLabel. And as far as I know, there isn't a method to get the name of the JLabel object.
    ... except for the getName() method of course.

    Also if all the jlabels are in a container (say a panel) then you can use
    
    String stringentered = "XXX";
    JLabel activelabel = null;
    
    Component[] components = panel.getComponents();
    
    for (int i=0; i<components.length;i++) {
        if (components[i] instanceof JLabel && stringentered.equals(components[i]) ) {
              activelabel = (JLabel)components[i];
              break;
        }
    }
    
    
    


  • Registered Users Posts: 3,357 ✭✭✭snappieT


    I think we need to go back to the start here...
    Good idea.

    I have a line of boxes, numbered from 1 to 100, named p1, p2, p3... p100. Dice rolled. The appropriate box needs to be highlighted (change colour, whatever) when the dice is rolled.
    So I have an integer value between 1 and 6, and this is added to the existing square that I'm on (counter). So now I know that I need to make the square called, say p54, become highlighted.
    I therefore have a string (called updateSquare), the contents of which have the same name as a JLabel object that represents that particular square.
    I need to run p54.makeHighlighted(), but I only have a String with p54 contained within.

    More clear?
    Possible?

    Please Help.


  • Registered Users Posts: 1,996 ✭✭✭lynchie


    As was already mentioned.. easiest way is to add all your JLabels to a HashMap using the string as the key, e.g. in your example use map.put("p54",p54);


  • Advertisement
Advertisement