Concordance2 - */ public static void main( String [ ] args...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.util.*; import java.io.*; import DataStructures.*; import DataStructures.Comparable; import DataStructures.LinkedList; i class WordEntry implements Comparable { String word; LinkedList lines; LinkedListItr listEnd; public int compareTo( Comparable rhs ) { return word.compareTo( ((WordEntry)rhs).word ); } public String toString( ) { StringBuffer theString = new StringBuffer( ); // Print the word theString.append( word + ":" ); // Now print the line numbers { LinkedListItr itr = lines.first( ); theString.append( "\t" + itr.retrieve( ) ); for( itr.advance( ); !itr.isPastEnd( ); itr.advance( ) ) theString.append( ", " + itr.retrieve( ) ); } return new String( theString ); } } class Concordance2 { /** * 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.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: */ public static void main( String [ ] args ) { try { BufferedReader inFile = new BufferedReader( new FileReader( args[0 ] ) ); AvlTree wordMap = new AvlTree( ); String oneLine; WordEntry entry = new WordEntry( ); // Read the words; add them to wordMap for( int lineNum = 1; ( oneLine = inFile.readLine( ) ) != null; lineNum++ ) { StringTokenizer st = new StringTokenizer( oneLine ); while( st.hasMoreTokens( ) ) { entry.word = st.nextToken( ); WordEntry match = (WordEntry) wordMap.find( entry ); if( match == null ) { match = new WordEntry( ); match.word = entry.word; match.lines = new LinkedList( ); match.listEnd = match.lines.zeroth( ); wordMap.insert( match ); } match.lines.insert( new Integer( lineNum ), match.listEnd ); match.listEnd.advance( ); } } wordMap.printTree( ); } catch( Exception e ) { System.err.println( "Error: " + e ); } } }...
View Full Document

This document was uploaded on 06/13/2011.

Page1 / 2

Concordance2 - */ public static void main( String [ ] args...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online