/home/sjenks/NetBeansProjects/CS2/src/hashing/FrequencyCounter2.java
  1 /*
  2  * This take input and then puts it into a hash map and sorts it alphabetically
  3  */
  4 package hashing;
  5 
  6 import java.io.File;
  7 import java.io.FileNotFoundException;
  8 import java.util.HashMap;
  9 import java.util.Map;
 10 import java.util.Scanner;
 11 import java.util.Set;
 12 import java.util.TreeSet;
 13 import javax.swing.JFileChooser;
 14 
 15 /**
 16  *
 17  * @author sjenks
 18  */
 19 
 20 public class FrequencyCounter2 {
 21 
 22    private static Map<String, Integer> words = new HashMap<String, Integer> ();
 23 
 24     /**
 25      * @param args the command line arguments
 26      */
 27     public static void main(String[] args) throws FileNotFoundException {
 28         establish();
 29         dump();
 30        interpret();
 31     }
 32 
 33     private static void establish() throws FileNotFoundException {
 34         Scanner reader = getProperty();
 35         int counter = 1;
 36         while (reader.hasNext()) {
 37             String word = reader.next().toLowerCase();
 38             if (words.containsKey(word)) {
 39                 words.get(word);
 40                 counter = words.get(word) + 1;
 41                 words.replace(word, counter);
 42             } else {
 43                 counter= 1;
 44                 words.put(word, counter);
 45             }
 46         }
 47         
 48 
 49     }
 50     private static Scanner getProperty() throws FileNotFoundException {
 51         String homedir = System.getProperty("user.home");
 52         System.out.println("homedir = " + homedir);
 53         JFileChooser jfc = new JFileChooser(new File(homedir));
 54         jfc.showOpenDialog(null);
 55         File file = jfc.getSelectedFile();
 56         Scanner scanner = new Scanner(file);
 57         return scanner;
 58     }
 59 
 60     private static void dump() {
 61         for (String wordName : words.keySet()) {
 62             System.out.println(">" + wordName + ":" + words.get(wordName)+ ">");
 63         }
 64     }
 65 
 66     private static void interpret() {
 67         System.out.println("Command?");
 68         Scanner interpreter = new Scanner(System.in);
 69         String command = interpreter.next();
 70         if (command.equalsIgnoreCase("count")) {
 71             count();
 72         } else if (command.equalsIgnoreCase("print")) {
 73             print();
 74         } else if (command.equals("exit")) {
 75             exit();
 76         } else {
 77             help();
 78 
 79         }
 80     }
 81 
 82     private static void count() {
 83         System.out.println("word?");
 84         Scanner scanner = new Scanner(System.in);
 85         String key = scanner.next();
 86         Integer value = words.get(key);
 87         System.out.println(key +"-->" + value);
 88         interpret();
 89     }
 90 
 91     private static void print() {
 92         Set<String> wordSet= new TreeSet<String>();
 93         for (String name: words.keySet()){
 94             wordSet.add(name);}
 95         for(String name: wordSet){
 96            System.out.println("<" + name +":" + words.get(name)+">");
 97         }
 98         interpret();
 99     }
100 
101     private static void exit(){
102 
103     }
104 
105     private static void help() {
106         System.out.println("Sorry, did not recognize the command your options are: \n"
107                 + "count|print|exit|help");
108         interpret();
109     }
110 }
111