Title: Cross Reference program
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Cross Reference program

This example demonstrates some of the power of a TreeMap to provide the "heavy lifting" for a Cross Reference program.

CrossReference.java

package cross_reference;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class CrossReference {

    private String fileName;
    private TreeMap<String, LinkedList<Integer> > wordInfoTree;

    public CrossReference(String fileName)
    {
        this.fileName = fileName;
        wordInfoTree = new TreeMap<String, LinkedList<Integer> >();
    }

    private void add(String word, int lineNumber)
    {
        LinkedList<Integer> linkedList = wordInfoTree.get(word);
        if (linkedList != null)
        {
            linkedList.add(lineNumber);
        }
        else
        {
            linkedList = new LinkedList<Integer>();
            linkedList.add(lineNumber);
            wordInfoTree.put(word, linkedList);
        }
    }
    public void process_printListing(PrintStream ps)
    {
        Scanner input;
        try {
            input = new Scanner(new File(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("Unable to open fileName: " + fileName);
            return;
        }
        int lineNumber=1;
        while (input.hasNextLine())
        {
            String line = input.nextLine();
            ps.println(lineNumber +": " + line);
            StringTokenizer st = new StringTokenizer(line, " .,;:<>(){}=[]+-/*!\"%\t");
            while (st.hasMoreTokens())
            {
                String word = st.nextToken();
                add(word, lineNumber);
            }
            lineNumber +=1;
        }

        input.close();
    }

    public void printXrefListing(PrintStream ps)
    {
        Set<String> words = wordInfoTree.keySet();

        // Iterate through the words (keys to wordInfoTree)
        Iterator<String> iter = words.iterator();
        while (iter.hasNext())
        {
            String word = iter.next();
            LinkedList<Integer> linkedList = wordInfoTree.get(word);
            ps.print(word + " : ");

            // Iterate through the line numbers for this word
            Iterator<Integer> lineIter = linkedList.iterator();
            int count = 0;
            while(lineIter.hasNext())
            {
                int lineNumber = lineIter.next();
                ps.print(lineNumber+ " ");
                count += 1;
                if ((count % 10) == 0)
                {
                    ps.println();
                    ps.print("     "); // Space at the beginning of a new line
                }
            }
            ps.println();
        }
    }

    public static void main(String[] args) {
        CrossReference cr = new CrossReference("src/cross_reference/CrossReference.java");
        cr.process_printListing(System.out);
        System.out.println("******   Cross  Reference     *******************");
        cr.printXrefListing(System.out);
    }

}



Last Updated: March 8, 2014 10:21 PM