Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Anyone know How to solve this basic problem?

Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My java program saves the words from ten text files into a tree map called frequencyData (it also records the frequency of each word).

There is a for loop to read each document file and the names of each file are stored in a string array (Docs).

How can I change the program so that a tree map is made for each document file? [I'm guessing a will have to create a new tree map within the for loop.]

Also, How do I determine the name of each new tree map? (eg. frequencyData1, frequencyData2....frequencyData10)

author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This "how do I determine the variable name" thing is a fundamental milestone that all thinking programmers get to -- some earlier, some later. The fact that you see the problem, and are trying to find a solution, is a good sign!

In any case, the important thing is that there is no solution, at least not the kind you're looking for. You can't invent variables at runtime. The compiler has to know the name of a variable, which means you have to know the name while you're typing the program -- and since you don't, for example, know how many files will be read, you don't know the names.

Now, the good news is that all it takes is looking at the problem from a slightly different angle, and I think when I say this you will slap your forehead at its obviousness: the solution to the "how do I make up variable names at runtime" question is always "use a Collection." Use, in your case, a List of Maps -- or perhaps a Map of Maps, with the filenames as keys and the word maps as the values. Then each Map has a "name" in the form of its key in the map.
Where does a nanny get ground to air missles? Protect this tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic