• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamically Creating Variables

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a very similar strategy that you defined above, using a Map (or HashMap). For example:
 
John Wilkinson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, sorry

 
John Wilkinson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You nailed it Steve! Many thanks for your help.
Regards
John
 
reply
    Bookmark Topic Watch Topic
  • New Topic