Concordance - { lines = new LinkedList( ); wordMap.put(...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.util.*; import java.io.*; i class Concordance { /** * A main routine that: * 1. Reads a file containing strings (from args[0]) * 2. Prints each string, and the lines on which it occurs * Skimpy error checks to avoid clogging up the code. */ public static void main( String [ ] args ) { try { BufferedReader inFile = new BufferedReader( new FileReader( args[0] ) ); TreeMap wordMap = new TreeMap( ); String oneLine; // Read the words; add them to wordMap for( int lineNum = 1; ( oneLine = inFile.readLine( ) ) != null; lineNum++ ) { StringTokenizer st = new StringTokenizer( oneLine ); while( st.hasMoreTokens( ) ) { String word = st.nextToken( ); LinkedList lines = (LinkedList) wordMap.get( word ); if( lines == null )
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { lines = new LinkedList( ); wordMap.put( word, lines ); } lines.addLast( new Integer( lineNum ) ); } } // Go through the word map for( Iterator itr = wordMap.entrySet( ).iterator( ); itr.hasNext( ); ) printEntry( (Map.Entry) itr.next( ) ); } catch( Exception e ) { System.err.println( "Error: " + e ); } } public static void printEntry( Map.Entry entry ) { // Print the word System.out.println( entry.getKey( ) + ":" ); // Now print the line numbers Iterator itr = ( (LinkedList)(entry.getValue( )) ).iterator( ); System.out.print( "\t" + itr.next( ) ); while( itr.hasNext( ) ) System.out.print( ", " + itr.next( ) ); System.out.println( ); } }...
View Full Document

This document was uploaded on 06/13/2011.

Ask a homework question - tutors are online