Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Tree Maps with 3 variables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tree Maps with 3 variables?" Watch "Tree Maps with 3 variables?" New topic

Tree Maps with 3 variables?

Conrad McLaughlin

Joined: Jan 31, 2006
Posts: 27
The program below is fully working and reads two documents (words.txt and words2.txt). The words from both docs are stored in the tree map 'frequency data'.

Example of system output :
3 the

Which means 'the' appears three times in both docs combined.

I want to know how many times it appears in each text document.

I have tried to add another integer by doing: TreeMap<String, Integer, Integer>. So that I can give document but this does not work. Can I do this?

import java.util.*; // Provides TreeMap, Iterator, Scanner
import*; // Provides FileReader, FileNotFoundException

public class WordCounter
public static void main(String[ ] args)
TreeMap<String, Integer> frequencyData = new TreeMap<String, Integer>( );


public static int getCount
(String word, TreeMap<String, Integer> frequencyData)
if (frequencyData.containsKey(word))
{ // The word has occurred before, so get its count from the map
return frequencyData.get(word); // Auto-unboxed
{ // No occurrences of this word
return 0;

public static void printAllCounts(TreeMap<String, Integer> frequencyData)
System.out.println(" Occurrences Word");

for(String word : frequencyData.keySet( ))
System.out.printf("%15d %s\n", frequencyData.get(word), word);


public static void readWordFile(TreeMap<String, Integer> frequencyData)
Scanner wordFile;
String word; // A word read from the file
Integer count; // The number of occurrences of the word
Integer doc; // The doc number from array of docs ***
int x;

for (x=0; x<Docs.length; x++) //For loop to read the documents
{ //***

//wordFile = new Scanner(new FileReader("words.txt"));
wordFile = new Scanner(new FileReader(Docs[x]));
catch (FileNotFoundException e)

while (wordFile.hasNext( ))
// Read the next word and get rid of the end-of-line marker if needed:
word = );

// Get the current count of this word, add one, and then store the new count:
count = getCount(word, frequencyData) + 1;
frequencyData.put(word, count);
} //****


// Array of documents
static String Docs [] = {"words.txt","words2.txt"}; //Docs are stored here

marc weber

Joined: Aug 31, 2004
Posts: 11343

You could create a data-encapsulating object to hold as many parameters as you wish. For example...

Then your TreeMap would be of type TreeMap<String, WordFrequency>.

PS: Please use Code Tags to keep your formatting intact.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: Tree Maps with 3 variables?
It's not a secret anymore!