/home/sjenks/NetBeansProjects/CS2/src/trees/FrequencyCounter.java |
1
2
3
4
5 package trees;
6
7 import java.io.File;
8 import java.io.FileNotFoundException;
9 import java.io.IOException;
10 import java.util.Scanner;
11 import javax.swing.JFileChooser;
12
13
14
15 @author
16
17 public class FrequencyCounter {
18
19
20 @param args
21
22 public static void main(String[] args) throws FileNotFoundException, IOException, BinaryTreeCreationException {
23 Scanner scanner = getProperty();
24 BinaryTree<String, Integer> tree = addLeaves(scanner);
25 display(tree);
26
27 }
28
29 private static Scanner getProperty() throws FileNotFoundException {
30 String homedir = System.getProperty("user.home");
31 System.out.println("homedir = " + homedir);
32 JFileChooser jfc = new JFileChooser(new File(homedir));
33 jfc.showOpenDialog(null);
34 File file = jfc.getSelectedFile();
35 Scanner scanner = new Scanner(file);
36 return scanner;
37 }
38
39 private static BinaryTree<String, Integer> addLeaves(Scanner scanner) throws FileNotFoundException, BinaryTreeCreationException {
40 BinaryTree<String, Integer> t = new BinaryTree<String,Integer> ();
41 int count = 1;
42 while (scanner.hasNext()){
43 String word = scanner.next().toLowerCase();
44 if (t.member(word)){
45 count = t.get(word);
46 count= count + 1;
47 t.find(word).setValue(count);
48 }else{
49 count=1;
50 t.addST(word,count);
51 }
52 }
53 return t;
54 }
55
56 private static void display(BinaryTree<String, Integer> tree) {
57 System.out.println(">>> Inorder");
58 tree.inorder();
59 }
60 }
61