#Trying to use a string as a variable name

48 messages · Page 1 of 1 (latest)

supple raven
rigid rampart
#

If the variables g1-g7 represent a name-to-value pair, then a hashmap makes sense. But if they instead represent a sequence of values, I would put them inside a collection of some sort (like an array) and iterate over the collection instead.

Here's the difference:

Pairs:

HashMap<String, String> pairs = new HashMap<>();

for (int i = 1; i <= 7; i++) {
  pairs.put("g" + i, "Works");
}

System.out.println(pairs);
// {g7=Works, g1=Works, g2=Works, g3=Works, g4=Works, g5=Works, g6=Works}

Sequence:

String[] seq = new String[7];
for (int i = 1; i <= 7; i++) {
    seq[i - 1] = "Works"; // array indices start at 0
}

System.out.println( Arrays.toString(seq) ); // Requires java.util.Arrays
// [Works, Works, Works, Works, Works, Works, Works]
#

you can't programmatically access the value of a variable using its name as a string, at least not easily. But you can do that if the names are keys that are mapped to values in a HashMap.

Again, it all depends on what your data actually represents. If the names really are tied to the values, then using a map is a good idea. But if it's just a bunch of values, then you should put them inside a collection (but only if they're related to each other somehow). If you don't need random access to the values here, you might not even need an array. If you always know which variable you need to use, you can refer to it in the program by it's name, like g3.

supple raven
#

I need to get the g1 variable in that case to use as an input, not a string

rigid rampart
#

HashMaps work with any data type, and the key class can be different than the value class, so that's not an issue

supple raven
#

In that case, what would I use in the <> of the Hashmap <String, ?>

rigid rampart
#

Yeah, HashMap<String, JFrame>

supple raven
# rigid rampart Yeah, `HashMap<String, JFrame>`

You set the value of the "g"+i , however, all the values of the g1-g100 are different, but of the same class (bad example given in image), rather I need not a set value of the second data type, but the name of it to be used in a println. For example, System.out.println(g1); above would print "Works" . All of the values of the g1-g100 have been set before, as in the image

rigid rampart
#

What are you trying to accomplish, exactly?

supple raven
#

For example, the error here would be "g"+1 cannot be converted to JFrame

#

The second pair in the .put

rigid rampart
#

But remember, if you just want 100 jframes, using an array is probably easier

#

But If you really need to access the names using strings, then the map is fine

supple raven
#

What if there were separate values in the JFrame(#,#) that are different for all 100

rigid rampart
supple raven
#

It is a separate class file, those numbers are taken as arguments, random numbers that would not work in a for loop - which is why I defined the variables before with set numbers

rigid rampart
#

Can you show me what you mean? What does the class look like?

supple raven
#

This method in a separate W45 file

rigid rampart
#

That doesn't look right, where are those variables declared?

supple raven
#

The variables are declared previously in the file - the image just shows that numbers are taken in as arguments. The numbers are given from a different class file, for example W45 name = new W45(#,#);

#

W45 is a separate file with hundreds of lines of code

rigid rampart
#

Okay, in your main program, how do you access those classes?

supple raven
#

My idea is simple, but might just be impossible.. for example Sos(g1); Sos(g2); Sos(g3); works because g1-3 are variable names

supple raven
#

What I could do is write hundreds of lines of code of Sos.() it would work fine

#

I am trying to find a solution with a for loop to see if I can shorten it

rigid rampart
#

Using "g" + i again

#

Does your program really have 100 lines of global variables like that?

supple raven
#

Yes, more - is it unusual?

#

I have to keep it as global variables

rigid rampart
#

With that many, I would normally store the numbers in a file for eaiser organization, also it really seems like a list or array would be more useful than variables

supple raven
#

Because of the program I am using, I have to keep it as global variables. Using what you have shown, I could rather have a HashMap with hundreds of lines of .put (or not if it is in a for loop, and the #'s are random)

#

I am not having errors with my program, just trying to experiment to see if my idea is possible

rigid rampart
#

Well, It is technically possible to get a variable using a string, using a method called reflection. It's not ideal because it technically breaks the way the language was meant to be used (allows you to access private variables, etc) but it can be useful if this is your only option. Give me a minute and I'll write an example

#

Wait hold on, I might be thinking of instance variables (fields) not local function variables. Are the variables g1-100 declared at class-level (outside of any methods) or are they local variables in a function like main()?

supple raven
rigid rampart
#

Darn it, yeah reflection won't work for that part, nevermind

#

In that case, you'll have to either create a map/array and manually put all those variables in there, or just use the variable names directly like you've been doing

supple raven
#

Putting variables into an array, then using a for loop to call it, seems that I would already save hundreds of lines of code

#

SOmehow I did not think of it

#

Although even the array would be 100+ different variables

rigid rampart
# supple raven

It looks like a lot of these have the same numbers. You could easily make a loop for all the objects with the same values and put them in an array

rigid rampart
#

Still 100 objects, but now you have an easy place to access them

supple raven
#

Thank you