File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

Anyone know How to solve this basic problem?

Anthony Alexander

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

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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:
subject: Anyone know How to solve this basic problem?
It's not a secret anymore!