UserPreferences

Sample9


Open a file, put it in string, get it one word at a time, and then put each word in a hash table.
// Who wrote Shakespeare's plays?
// Putting it all together
// D.S. Blank

import java.util.*;
import java.io.*; 

// A class that encapsulates:
// - the file reading
// - the hashtable
// - the data from the file
// - getting a word from a string

class Bard {
    Map<String, Integer> word;
    int wordCount;
    String data;
    int current;
    boolean DONE;
    // Constructor, initializes everything:
    public Bard() {
        word = new HashMap<String, Integer>();
        wordCount = 0;
        data = "";
        current = 0;
    }
    // Gets a word from data, stores it in dictionary:
    public String getWord() {
        String nextword = "";
        // get the next word:
        while (current < data.length() && 
               data.charAt(current) != ' ') {
            nextword += data.charAt(current);
            current++;
        }
        while (current < data.length() && 
               data.charAt(current) == ' ') {
            current++;
        }
        // now add it to the dictionary:
        if (!nextword.equals("")) {
            System.out.println("Adding '" + nextword + "' to dictionary...");
            if (word.containsKey(nextword)) {       
                word.put(nextword, 
                         word.get(nextword)+1);
            } else {
                word.put(nextword, 1);
                wordCount++;
            }
        }
        if (current >= data.length())
            DONE = true;
        return nextword;
    }
    // Opens a file, stores contents in data:
    public void openFile(String filename) { 
        FileReader in = null; 
        try { 
            in = new FileReader(filename); 
        } catch (FileNotFoundException e) { 
            System.err.println("CheckedIOTest: " + e); 
            System.exit(-1); 
        } catch (IOException e) { 
            System.err.println("CheckedIOTest: " + e); 
            System.exit(-1); 
        } 
        int c; 
        try {
            while ((c = in.read()) != -1) { 
                if ((char)c > 'A' && (char)c < 'z') 
                    data += (char) c; 
                else
                    data += " "; 
            } 
        } catch (IOException e) { 
            System.out.println("Done!");
        } 
        try {
            in.close(); 
        } catch (IOException e) { 
            System.out.println("Done!");
        } 
    } 
    // Replicates a character c times:
    public static String repl(char c, int n) {
        char[] foo = new char[n];
        for (int i = 0; i < n; i++) {
            foo[i] = c;
        }
        return new String(foo);
    }

    public void listWords() {
        for (Map.Entry<String, Integer> e : word.entrySet()) {
            System.out.print((e.getKey() + repl(' ', 30)).substring(0, 30));
            System.out.println("    " + e.getValue());
        }
    }

    public static void main(String args[]) {
        Bard myBard = new Bard();
        myBard.openFile( args[0] );
        while (! myBard.DONE) {
            myBard.getWord();
        }
        myBard.listWords();
    }
}