• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Variable variable names

txrandom

Diamond Member
Say I have an array of Strings like {abc, def, ghi, jkl, mno} and I want to make a list of variables with names nameabc, name def, nameghi, namejkl, namemno. Is it possible to make Strings as variable names?

This is all in Java btw.
 
Disclaimer: I know not the Java.

I don't see why you'd want to do that? As in, you have a load of variables, and you want to make variables out of them? Why not just use the original variables in the array to store whatever values it is you would assign to the variable variables?
 
Don't know about java but you can do it in php so maybe this will help.

foreach ($array as $key) {
$key = 'name'.$key;
}

to call the variable use $$key.
 
I think I asked the wrong question, but it's the same concept. My JAVA program takes a year and finds the Friday the 13ths in that year. To do this, I do:

Calendar calendar1 = new GregorianCalendar(year, Calendar.JANUARY, 13);

day = calendar1.get(Calendar.DAY_OF_WEEK);

If the day equals 6, I print out the full date.

I repeat this for each month. It's a pretty simple problem, but I thought I'd be able to run it through a loop where the MONTH is being changed.

I thought I could just do a foreach through an array with JANUARY...DECEMBER and make the JANUARY in Calendar.JANUARY change to the other months.




 
Originally posted by: jjones
Don't know about java but you can do it in php so maybe this will help.

foreach ($array as $key) {
$key = 'name'.$key;
}

to call the variable use $$key.

I originally started my main programming in PHP, so I knew you could do it in PHP. That's why I thought it might be possible in JAVA, I asked my professor if he knew how to do it and he said no.
 
I know in JavaScript and Perl you can do something like eval(var $varvar) (ignore the syntax) and that will turn whatever $varvar is into a variable. I don't know if Java has a similar function

edit: oh hey look!
 
Back
Top