#Trying to use a string as a variable name
48 messages · Page 1 of 1 (latest)
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.
I get what your saying, but it is not what I was hoping for. In what I am trying to create, the g1-g100 are all the same type, but of a class. Example: JFrame g1 = new JFrame(); the next is g2= new JFrame();
I need to get the g1 variable in that case to use as an input, not a string
HashMaps work with any data type, and the key class can be different than the value class, so that's not an issue
In that case, what would I use in the <> of the Hashmap <String, ?>
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
What are you trying to accomplish, exactly?
For example, the error here would be "g"+1 cannot be converted to JFrame
The second pair in the .put
Replace the second "g" + i with new JFrame() if you're using those as the objects
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
What if there were separate values in the JFrame(#,#) that are different for all 100
Where are you getting those numbers from? Is it random, in a file or somewhere else? Either way, you can iterate over the source of those numbers and use them for that
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
Can you show me what you mean? What does the class look like?
This method in a separate W45 file
That doesn't look right, where are those variables declared?
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
Okay, in your main program, how do you access those classes?
My idea is simple, but might just be impossible.. for example Sos(g1); Sos(g2); Sos(g3); works because g1-3 are variable names
W45 name = new W45(#,#);
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
If you use a HashMap instead of global variables, you can do Sos( pairs.get("g1") ); etc and use a loop for that.
Using "g" + i again
Does your program really have 100 lines of global variables like that?
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
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
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()?
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
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
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
An array would only be 1 variable, much easier to manage
Still 100 objects, but now you have an easy place to access them
Thank you