TCLUG Development Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG-DEVEL:216] Java - dynamically generate variable names



This problem just screams out for a container class.  One of the various hash
classes should work well.  Example:

Hashtable varHash = new Hashtable(20);  // set initial capacity to 20
String key_static = kwdph;
int arg_cnt = 1;
while(true)
{
	String key = key_static + String.valueOf(arg_cnt);
	varHash.put(key, parser.getStringParameter("phrase" + arg_cnt));
	if(varHash.get(key) == null)
		break;
	arg_cnt++;
}

-Chris

On Wed, 29 Mar 2000, Erick Stohr wrote:

> Java
> 
> 
> I am trying to dynamically generate an unknown amount of new variables,
> all with different names e.g. b1 b2 b3 b4, within a for loop. My current
> attempt is this
> 
> 
>                                                 // for loop to get
> parameters
>                                         for(int i = 1; i <=20; ++i)
>                                         {
>                                          String kwdph +
> String.valueOf(i) = parser.getStringParameter("phrase" + i);
> 
>                                           if ( kwdph + String.valueOf(i)
> == null)
>                                                         break;
> 
>                                         } // end of for
> 
> 
> But obviosly it doesn't work. Does anyone know of any other way of doing
> this? Thanks.
> 
> Erick
> 
> --
> _______________________________________________
> 
> Erick Stohr - PageLab Network, Inc.
> mailto:erick@pagelab.net  http://pagelab.net
> 
> Your search-to-sale global e-commerce solution!
> 
> Toll free 1-800-447-0607
> Voice 612-362-9224
> Fax 612-362-9227
> 43 Main Street SE, Suite 228
> Minneapolis, Minnesota 55414
> _______________________________________________
> 
> 
>