From 6c7e72929abc0778ad4bda47319364924d9f36c1 Mon Sep 17 00:00:00 2001 From: khoick245 Date: Sun, 29 Nov 2015 13:31:12 -0800 Subject: [PATCH 1/2] Create CountWords --- CountWords | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 CountWords diff --git a/CountWords b/CountWords new file mode 100644 index 0000000..9de77fe --- /dev/null +++ b/CountWords @@ -0,0 +1,95 @@ +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Map; +import java.util.Scanner; +import java.util.Set; +import java.util.TreeMap; + +/** + * This class is create to count the number of work in a book + * @author khoi + * + */ +public class Q2 { + + private Map bookMap; // hold book + + Q2() { + bookMap = new TreeMap(); + } + + /** + * This method is to get the path of file + * + * @return A path of file + */ + public static String getFileName() { + String filename = null; + Scanner in = new Scanner(System.in); + System.out.println("Enter file to process: "); + if (in.hasNext()) { + filename = in.next(); + } + in.close(); + return filename; + } + + /** + * This method read all content of a book and put them into a array list + * @return Arraylist + */ + public ArrayList readfile() { + + ArrayList book = new ArrayList(); + + try { + String filename = getFileName(); // get path of file + System.out.println(filename); + File reader = new File(filename); + Scanner in = new Scanner(reader); + + while (in.hasNext()) { + book.add(in.next().toLowerCase()); + } + } catch (Exception e) { + e.printStackTrace(); + } + return book; + } + + public Map getBookMap() { + return bookMap; + } + + public void setBookMap(Map bookMap) { + this.bookMap = bookMap; + } + + public static void main(String[] args) { + + Q2 objQ2 = new Q2(); + + // read book + ArrayList st = new ArrayList(); + st = objQ2.readfile(); + + Set unique = new HashSet(st); + for (String key : unique) { + objQ2.getBookMap().put(key, Collections.frequency(st, key)); // put work and number of its into map + } + + // show content of book + System.out.println("\n"); + for (String s : objQ2.getBookMap().keySet()) + System.out.println(s + " : " + objQ2.getBookMap().get(s)); + + // show biggest value + System.out.println("Biggest value:"); + int t = Collections.max(objQ2.getBookMap().values()); + for (String s : objQ2.getBookMap().keySet()) + if (objQ2.getBookMap().get(s) == t) + System.out.println(s + " : " + objQ2.getBookMap().get(s)); + } +} From bd89629605489b7d96a48253e659ada508e3c0a8 Mon Sep 17 00:00:00 2001 From: khoick245 Date: Fri, 12 Feb 2016 21:53:59 -0800 Subject: [PATCH 2/2] Update Driver --- Driver | 1 + 1 file changed, 1 insertion(+) diff --git a/Driver b/Driver index a6ecbe9..4cc642a 100644 --- a/Driver +++ b/Driver @@ -1,3 +1,4 @@ +// Khoi Nguyen import java.util.HashMap; import java.util.Iterator; import java.util.Map;