aspose file tools*
The moose likes Java in General and the fly likes Problem with declaring variables dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with declaring variables dynamically" Watch "Problem with declaring variables dynamically" New topic
Author

Problem with declaring variables dynamically

GaganSinghSanto patiala
Greenhorn

Joined: Feb 08, 2010
Posts: 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
Posts: 110
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
Posts: 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
Rancher

Joined: May 21, 2006
Posts: 425

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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10532
    
    9

ringer smack wrote:

Please check your private messages for an important administrative matter


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
GaganSinghSanto patiala
Greenhorn

Joined: Feb 08, 2010
Posts: 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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10532
    
    9

Sent you another private message. Please check.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

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 8 API documentation
GaganSinghSanto patiala
Greenhorn

Joined: Feb 08, 2010
Posts: 4
Thanks a lot for the help.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with declaring variables dynamically