0

I am fairly new to java, so don't judge me here.

I am trying to "dynamically" create and name some objects in a while loop.


I am reading in from a text file, which is in the format:

name, number
name, number
name, number
etc.

The code I have so far is this:

int agencyCount = 0;

while ((line = buff.readLine()) != null) {
     agencyCount ++; //how many agencies in the file
     String[] ar = line.split(", "); //split the text file, ar[0] = name, ar[1] = number
     String agNm = ar[0];
     int agNo = Integer.parseInt(ar[1]); //parse ar[1] to String
     String objectName = ("agency" + agencyCount); //set objectName, i.e. "agency1", "agency2", etc
     AgencyDetails agency = new AgencyDetails(agNm,agNo);
}

It all works, it's just telling me I'm not allowed to "dynamically" name objects. I now know this is due to java being a compiled language, so all variable names must be resolved at compile time.

I have searched far and wide for a solution. I have seen someone who recommended making an array, but I wasn't sure how I'd go about implementing that solution. Does anyone either know how to solve my issue, or know how I'd go about implementing an array to solve it?

5
  • 3
    Consider using a Map. Commented Feb 4, 2014 at 18:02
  • 1
    What is the exact error you're seeing? And can you post some example code that we can run ourselves instead of a snipped that won't compile? Commented Feb 4, 2014 at 18:02
  • 4
    If you use a Map you'll be able to "name" each element (in fact you assign a key to each element, not a name) Commented Feb 4, 2014 at 18:03
  • Or don't name them. If you have just incrementing numbers, use a List, your number is the index in that list. Commented Feb 4, 2014 at 18:07
  • 1
    Hi there. Just like @KevinWorkman mentioned, what errors are you seeing? It appears you are not using "objectName". Do you have more code to show us what you are trying to do? Thanks! Commented Feb 4, 2014 at 18:11

3 Answers 3

2

Before the loop you want to make an ArrayList.

ArrayList<AgencyDetails> agencyList = new ArrayList<AgencyDetails>();

Then, in the last line of your while-loop you want to add your new AgencyDetails to the ArrayList.

agencyList.add(agency);

After that, you have all your AgencyDetails in your ArrayList.

(you might want to give objectName to the AgencyDetails instead of agNm)

regards -Kayz

Sign up to request clarification or add additional context in comments.

1 Comment

Or use a Map for a similar effect keeping the names separate from the objects. In short, you can only simulate named variables.
1

I don't know what is your programming background, so I'll assume here that you have close to none. If this isn't the case, don't take this as an insult ;)

The answer to your question really depends on what you really want. Do you want a variable name that represents what your object is, or you want a permanent name on your data that will be bound to it for the whole object's lifetime?

In the first case, then the answer is no, this isn't possible. As you have mentionned, you need to define your variable's name before you compile your code. Dynamic variable names doesn't make sense in this context, since the variable's name is only valid in its scope. Once you get outside the scope where the variable was defined, then the name doesn't exist anymore.

For example:

public void someMethod () {
    SomeObject myNameIsJonas = createSomeObject ();
}

private SomeObject createSomeObject () {
    SomeObject myNameIsMud = new SomeObject ();
    return myNameIsMud;
}

Inside the method createSomeObject, the variable that holds the created object is called myNameIsMud. Once you exit the method and return the object, it is assigned to a variable called myNameIsJonas. So, even if you had dynamic variable names support in Java, it wouldn't really matter since the variable myNameIsMud cease to exist once you exit the method createSomeObject.

However, if what you want is some sort of permanent tag on the object, it is easily achieved. You can add a private class member inside your class (AgencyDetails), and set it either from the constructor, or from a setter method (setAgencyName (String name)). Then you can use this name as the key for a Map.

public class AgencyDetails {
    private String name = "";

    public AgencyDetails (String name, int number) {
        this.name = name;
    }

    public String getName () {
        return name;
    }
}

public class LesClaypool {
    private Map<String, AgencyDetails> agencyMap = new HashMap<String, AgencyDetails> ();

    public void doWork () {
        // Read agency info from file into nameFromFile and numberFromFile
        AgencyDetails agency = new AgencyDetails (nameFromFile, numberFromFile);
        agencyMap.put (agency.getName (), agency);
    }

    public AgencyDetails getAgencyDetailsByName (String name) {
        return agencyMap.get (name);
    }
}

After you have read the agency details information from your file, you can store them into a Map, using the name as the key. You can then get the AgencyDetails instance you are interested in by providing its name to the getAgencyDetailsByName method, as you can see with the simple example above.

Comments

0

I would recommend you using a Map so that you can associate each object with an object name. You could use something like Map<String,AgencyDetails>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.