| Author |
Dynamically Creating Variables
|
John Wilkinson
Greenhorn
Joined: Apr 03, 2009
Posts: 3
|
|
Hi Guys,
this is my first post and my first real stab at getting to grips with Java. Here's what I'm trying to do:
I have a file that I read line-by-line containing a bunch of User Names and Applications that each User has used. I need a way of counting the number of times each App for any given User has been used. Sounds simple eh? File would look something like this....
User: John
App: Excel
App: Excel
App: Word
App: Outlook
-----------------
User: Fred
App: Word
App: Word
----------------
User: Steven
App: Excel
App: Word
In perl this would be easy. I'd create a Hash Array and dynamically name each key as "User.AppName" (John.Excel, John.Outlook, Fred.Word etc) and then assign each a value based on some counter that i increment, so in the above example, Fred.Word=2. Having read some posts on here and also on other sites, I see that you cannot dynamically create variables in this way. I was wondering if anyone had any ideas as to how I might go about solving this problem..?
Many thanks
Johny
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3032
|
|
You could use a very similar strategy that you defined above, using a Map (or HashMap). For example:
|
Steve
|
 |
John Wilkinson
Greenhorn
Joined: Apr 03, 2009
Posts: 3
|
|
Hi Steve,
I think I may have jumped the gun on this. My explanation was not clear. It's the Application Count that I need a Dynamic Variable for. Once I have that, I can then use a HashMap to store the data, as you suggested. So, in my example file, I would need a way of defining an Int variable unique to each User.App pair - a variable that I increment as and when. Creating the Key is not what I'm after - it's finding a way to store the App count value and increment it...... JohnExcel++ etc
Sorry for any confusion.
John
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3032
|
|
Oh, sorry
|
 |
John Wilkinson
Greenhorn
Joined: Apr 03, 2009
Posts: 3
|
|
You nailed it Steve! Many thanks for your help.
Regards
John
|
 |
 |
|
|
subject: Dynamically Creating Variables
|
|
|