File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Tree Maps with 3 variables?

Conrad McLaughlin
Greenhorn

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 java.io.*; // Provides FileReader, FileNotFoundException

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

readWordFile(frequencyData);
printAllCounts(frequencyData);
}

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
}
else
{ // No occurrences of this word
return 0;
}
}

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

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

System.out.println("-----------------------------------------------");
}

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

try
{
//wordFile = new Scanner(new FileReader("words.txt"));
wordFile = new Scanner(new FileReader(Docs[x]));
}
catch (FileNotFoundException e)
{
System.err.println(e);
return;
}

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

}
----------------------------------------------------------------------------
marc weber
Sheriff

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
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tree Maps with 3 variables?