wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Anyone know How to solve this basic problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Anyone know How to solve this basic problem?" Watch "Anyone know How to solve this basic problem?" New topic
Author

Anyone know How to solve this basic problem?

Anthony Alexander
Greenhorn

Joined: Feb 16, 2006
Posts: 15
Outline
-------
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).

Question
--------
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)





Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anyone know How to solve this basic problem?