Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Stack;
- import java.util.Queue;
- import java.util.LinkedList;
- import java.io.File;
- import java.io.FileNotFoundException;
- // This is to read files
- import java.util.Scanner;
- public class ADT {
- static Queue<String> line = new LinkedList<String>();
- public static void main(String[] args) throws FileNotFoundException {
- // System.out.println(matcher("(45 + 36)-5"));
- // System.out.println(matcher("((a)"));
- // System.out.println(matcher("map{key[a(4)]}{b([v])}"));
- // System.out.println(matcher("({)}"));
- //
- // Queue<String[]> scoop = new LinkedList<String[]>();
- // String[] flavor1 = {"mint", "chocolate"};
- //
- // scoop.add(flavor1);
- //
- // String[] flavor2 = {"strawberry", "vanilla"};
- // scoop.add(flavor2);
- //
- // String[] flavor3 = {"choc. chip cookie dough"};
- // scoop.add(flavor3);
- //
- // String[] flavor4 = {"mint", "lime", "lime"};
- // scoop.add(flavor4);
- //
- // String[] flavor5 = {"orange", "green tea"};
- // scoop.add(flavor5);
- //
- // order(scoop);
- System.out.print("\t\t\t\t\t\t"+"cookie\t\t"+"recipe\t\t"+"total");
- frequency("/Users/salikh/eclipse-workspace/adt/src/bettycrocker.txt", "cookie", "recipe");
- frequency("/Users/salikh/eclipse-workspace/adt/src/biggerbolderbaking.txt", "cookie", "recipe");
- frequency("/Users/salikh/eclipse-workspace/adt/src/cookiessf.txt", "cookie", "recipe");
- frequency("/Users/salikh/eclipse-workspace/adt/src/marthastewart.txt", "cookie", "recipe");
- frequency("/Users/salikh/eclipse-workspace/adt/src/mozilla.txt", "cookie", "recipe");
- frequency("/Users/salikh/eclipse-workspace/adt/src/tasteofhome.txt", "cookie", "recipe");
- frequency("/Users/salikh/eclipse-workspace/adt/src/thebakingchocolatess.txt", "cookie", "recipe");
- }
- /**
- * Uses stack to create regex like action to detect
- * syntax errors with brackets
- * @param brackets
- * @return boolean parenthesis are closed properly or not
- */
- public static boolean matcher(String brackets) {
- Stack<Character> stack = new Stack<Character>();
- for(int i = 0; i < brackets.length(); i++) {
- char bracketchar = brackets.charAt(i);
- if(bracketchar == '[' || bracketchar == '(' || bracketchar == '{' ) {
- stack.push(bracketchar);
- } else if(bracketchar == ']') {
- if(stack.isEmpty() || stack.pop() != '[') {
- return false;
- }
- } else if(bracketchar == ')') {
- if(stack.isEmpty() || stack.pop() != '(') {
- return false;
- }
- } else if(bracketchar == '}') {
- if(stack.isEmpty() || stack.pop() != '{') {
- return false;
- }
- }
- }
- return stack.isEmpty();
- }
- /**
- * Order method that adds new item to the queue
- * @param orders
- * @return
- */
- public static String order(Queue<String[]> orders) {
- while(!orders.isEmpty()) {
- String[] current_order = orders.remove();
- if (current_order[0].equals("12* choc. chip cookie dough")) {
- System.out.print("cone: 12* choc. chip cookie dough");
- continue;
- }
- if (line.isEmpty()) {
- System.out.println("cone: " + String.join(",",current_order));
- } else {
- String temp = "";
- for (String i: line) {
- temp += i+",";
- }
- System.out.println("cone: "+temp+String.join(",",current_order));
- }
- line.clear();
- for (int i=0; i<current_order.length; i++) {
- if (current_order[i].equals("mint") ||current_order[i].equals("green tea") || current_order[i].equals("lime")){
- line.add(current_order[i]);
- }
- else if (current_order[i].equals("choc. chip cookie dough")) {
- String[] temp = {"12* choc. chip cookie dough"};
- orders.add(temp);
- }
- }
- }
- return "" ;
- }
- /**
- * Returns the number of times the word are found
- * @param file
- * @param wordA
- * @param wordB
- * @return
- * @throws FileNotFoundException
- */
- public static int frequency(String file, String wordA, String wordB) throws FileNotFoundException {
- int word1 = 0;
- int word2 = 0;
- File file1 = new File(file);
- Scanner sc = new Scanner(file1);
- while (sc.hasNext()) {
- String nextToken = sc.next();
- if (nextToken.equalsIgnoreCase(wordA))
- word1++;
- if (nextToken.equals(wordB))
- word2++;
- }
- System.out.println();
- int total = word2 + word1;
- String[] filename = file.split("/");
- String name = filename[filename.length-1];
- String format = "%1$-50s%2$-15s%3$-15s%4$-15s\n";
- System.out.format(format, name, word1, word2, total);
- return total;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement