This week's book giveaway is in the Flex forum.
We're giving away four copies of Flex 4 in Action and have Tariq Ahmed, Dan Orlando, John C. Bland II & Joel Hooks on-line!
See this thread for details.
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Java in General
 
RSS feed
 
New topic
Author

Problem with declaring variables dynamically

GaganSinghSanto patiala
Greenhorn

Joined: Feb 08, 2010
Messages: 4


Hi,

Needed help with declaring variables dynamically,


String[] a = {"Cheese", "Pepperoni", "Black Olives"};

for(int arg=0;arg<a.length;arg++)
{
HashMap ------- =new HashMap();

}

I wanted the hashmap variable names to be Cheese Pappereni ..etc.


Thanks.
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Messages: 96

can you post the error that has occured.

To err is human,
To forgive is not company policy
GaganSinghSanto patiala
Greenhorn

Joined: Feb 08, 2010
Messages: 4

Thanks for the reply prassana.

Actually i dint had a code in place...just wanted to see if there is a way to do that thing.

Basically i want to name my variables according to values in the array,so that i use the a variable for a set of operations.

Hope i dint confuse you.

Thanks.
Pushkar Choudhary
Ranch Hand

Joined: May 21, 2006
Messages: 352

ringer smack wrote:
Hi,

Needed help with declaring variables dynamically,


String[] a = {"Cheese", "Pepperoni", "Black Olives"};

for(int arg=0;arg<a.length;arg++)
{
HashMap ------- =new HashMap();

}

I wanted the hashmap variable names to be Cheese Pappereni ..etc.


Thanks.


Welcome to Javaranch!

Please use code tags when you post any code so that it looks neat like this:

>
Maneesh Godbole
Bartender

Joined: Jul 26, 2007
Messages: 4241

ringer smack wrote:

Please check your private messages for an important administrative matter

work is the scourge of the drinking class
GaganSinghSanto patiala
Greenhorn

Joined: Feb 08, 2010
Messages: 4

Sorry , i missed to tag the code

Hi,

Needed help with declaring variables dynamically,




I wanted the hashmap variable names to be Cheese Pappereni ..etc.
Maneesh Godbole
Bartender

Joined: Jul 26, 2007
Messages: 4241

Sent you another private message. Please check.

work is the scourge of the drinking class
Jesper Young
Java Cowboy
Bartender

Joined: Aug 16, 2005
Messages: 7852

This is not the first time someone asks a similar question. You cannot declare variable names dynamically, that's not how Java works. Why do you want to do this?

Use a different kind of data structure. You could use a Map, for example, to store objects by name.



Java Beginners FAQ - JavaRanch SCJP FAQ
The Java Tutorial - Java SE 6.0 API documentation
GaganSinghSanto patiala
Greenhorn

Joined: Feb 08, 2010
Messages: 4

Thanks a lot for the help.

 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Java in General
 
RSS feed
 
New topic
IntelliJ open source