• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris

Tree Maps with 3 variables?

Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 java.io.*; // 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 = wordFile.next( );

// 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

Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
There's a way to do it better - find it. -Edison. A better tiny ad:
Low Tech Laboratory
    Bookmark Topic Watch Topic
  • New Topic