From a527bcca91748675f32dfa84cce7f75c2c0d3bfd Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Mon, 12 Mar 2018 10:51:35 -0400 Subject: [PATCH 01/12] ParenChecker part 1 --- src/main/java/io/zipcoder/ParenChecker.java | 29 ++++++++++ .../java/io/zipcoder/ParenCheckerTest.java | 54 +++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index caee675..62d828c 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,33 @@ package io.zipcoder; +import java.util.Stack; + public class ParenChecker { + + private Stack charStack; + + public ParenChecker(Stack charStack) { + this.charStack = charStack; + } + + public boolean logic() { + int parenCount = 0; + for (int i = 0; i < charStack.size(); i++) { + if (parenCount < 0) { + return false; + } + if (charStack.get(i).equals('(')) { + parenCount++; + } + if (charStack.get(i).equals(')')) { + parenCount--; + } + } + if (parenCount != 0) { + return false; + } else { + return true; + } + } + } diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 76aa3b6..cbbb631 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -3,6 +3,60 @@ import org.junit.Assert; import org.junit.Test; +import java.util.ArrayList; +import java.util.Stack; + public class ParenCheckerTest { + @Test + public void parenthesisChecker1() { + Stack stack = new Stack(); + ArrayList charArrayList = new ArrayList(); + charArrayList.add('1'); + charArrayList.add('d'); + charArrayList.add('('); + charArrayList.add('f'); + charArrayList.add('('); + charArrayList.add(')'); + charArrayList.add(')'); + stack.addAll(charArrayList); + ParenChecker parenChecker = new ParenChecker(stack); + Assert.assertTrue(parenChecker.logic()); + } + + @Test + public void parenthesisChecker2() { + Stack stack = new Stack(); + ArrayList charArrayList = new ArrayList(); + charArrayList.add('1'); + charArrayList.add('d'); + charArrayList.add('('); + charArrayList.add('f'); + charArrayList.add('('); + charArrayList.add(')'); + charArrayList.add(')'); + charArrayList.add(')'); + stack.addAll(charArrayList); + ParenChecker parenChecker = new ParenChecker(stack); + Assert.assertTrue(!(parenChecker.logic())); + } + + @Test + public void parenthesisChecker3() { + Stack stack = new Stack(); + ArrayList charArrayList = new ArrayList(); + charArrayList.add(')'); + charArrayList.add('1'); + charArrayList.add('d'); + charArrayList.add('('); + charArrayList.add('f'); + charArrayList.add('('); + charArrayList.add(')'); + charArrayList.add(')'); + charArrayList.add(')'); + stack.addAll(charArrayList); + ParenChecker parenChecker = new ParenChecker(stack); + Assert.assertTrue(!(parenChecker.logic())); + } + } \ No newline at end of file From 859fd81e3fbb498923a5ab89819412602324a266 Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Mon, 12 Mar 2018 11:59:15 -0400 Subject: [PATCH 02/12] Logic 'generified' to allow more characters, old tests still pass --- src/main/java/io/zipcoder/ParenChecker.java | 18 +++++++++++++++--- .../java/io/zipcoder/ParenCheckerTest.java | 6 +++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index 62d828c..32cdaae 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -10,16 +10,28 @@ public ParenChecker(Stack charStack) { this.charStack = charStack; } - public boolean logic() { + public boolean logicChecker() { + if ((logic('(', ')')) && + logic('{', '}') && + logic('[', ']') && + logic('<', '>') && + logic('"', '"') && + logic('\'', '\'')) { + return true; + } + return false; + } + + public boolean logic(char firstChar, char secondChar) { int parenCount = 0; for (int i = 0; i < charStack.size(); i++) { if (parenCount < 0) { return false; } - if (charStack.get(i).equals('(')) { + if (charStack.get(i).equals(firstChar)) { parenCount++; } - if (charStack.get(i).equals(')')) { + if (charStack.get(i).equals(secondChar)) { parenCount--; } } diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index cbbb631..f132979 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -21,7 +21,7 @@ public void parenthesisChecker1() { charArrayList.add(')'); stack.addAll(charArrayList); ParenChecker parenChecker = new ParenChecker(stack); - Assert.assertTrue(parenChecker.logic()); + Assert.assertTrue(parenChecker.logicChecker()); } @Test @@ -38,7 +38,7 @@ public void parenthesisChecker2() { charArrayList.add(')'); stack.addAll(charArrayList); ParenChecker parenChecker = new ParenChecker(stack); - Assert.assertTrue(!(parenChecker.logic())); + Assert.assertTrue(!(parenChecker.logicChecker())); } @Test @@ -56,7 +56,7 @@ public void parenthesisChecker3() { charArrayList.add(')'); stack.addAll(charArrayList); ParenChecker parenChecker = new ParenChecker(stack); - Assert.assertTrue(!(parenChecker.logic())); + Assert.assertTrue(!(parenChecker.logicChecker())); } } \ No newline at end of file From 91fca38219e510cf72ecf92230344ee1cf62c099 Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Mon, 12 Mar 2018 12:05:32 -0400 Subject: [PATCH 03/12] Part 2 done and tested --- .../java/io/zipcoder/ParenCheckerTest.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index f132979..fc03281 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -59,4 +59,84 @@ public void parenthesisChecker3() { Assert.assertTrue(!(parenChecker.logicChecker())); } + @Test + public void curlyBracketChecker() { + Stack stack = new Stack(); + ArrayList charArrayList = new ArrayList(); + charArrayList.add('1'); + charArrayList.add('d'); + charArrayList.add('{'); + charArrayList.add('f'); + charArrayList.add('('); + charArrayList.add('}'); + charArrayList.add(')'); + stack.addAll(charArrayList); + ParenChecker parenChecker = new ParenChecker(stack); + Assert.assertTrue(parenChecker.logicChecker()); + } + + @Test + public void bracketChecker() { + Stack stack = new Stack(); + ArrayList charArrayList = new ArrayList(); + charArrayList.add('1'); + charArrayList.add('d'); + charArrayList.add('['); + charArrayList.add('f'); + charArrayList.add('('); + charArrayList.add(']'); + charArrayList.add(')'); + stack.addAll(charArrayList); + ParenChecker parenChecker = new ParenChecker(stack); + Assert.assertTrue(parenChecker.logicChecker()); + } + + @Test + public void diamondChecker() { + Stack stack = new Stack(); + ArrayList charArrayList = new ArrayList(); + charArrayList.add('1'); + charArrayList.add('d'); + charArrayList.add('['); + charArrayList.add('f'); + charArrayList.add('<'); + charArrayList.add(']'); + charArrayList.add('>'); + stack.addAll(charArrayList); + ParenChecker parenChecker = new ParenChecker(stack); + Assert.assertTrue(parenChecker.logicChecker()); + } + + @Test + public void doubleQuoteChecker() { + Stack stack = new Stack(); + ArrayList charArrayList = new ArrayList(); + charArrayList.add('1'); + charArrayList.add('d'); + charArrayList.add('"'); + charArrayList.add('f'); + charArrayList.add('('); + charArrayList.add('"'); + charArrayList.add(')'); + stack.addAll(charArrayList); + ParenChecker parenChecker = new ParenChecker(stack); + Assert.assertTrue(parenChecker.logicChecker()); + } + + @Test + public void singleQuoteChecker() { + Stack stack = new Stack(); + ArrayList charArrayList = new ArrayList(); + charArrayList.add('1'); + charArrayList.add('d'); + charArrayList.add('\''); + charArrayList.add('f'); + charArrayList.add('('); + charArrayList.add('\''); + charArrayList.add(')'); + stack.addAll(charArrayList); + ParenChecker parenChecker = new ParenChecker(stack); + Assert.assertTrue(parenChecker.logicChecker()); + } + } \ No newline at end of file From d7e36b75a74113b61437233dc850b46f8809042f Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Mon, 12 Mar 2018 14:51:51 -0400 Subject: [PATCH 04/12] Logic progress --- src/main/java/io/zipcoder/WC.java | 26 ++++++++++++++++++++++++++ src/main/resources/someTextFile.txt | 3 +++ src/test/java/io/zipcoder/WCTest.java | 6 ++++++ 3 files changed, 35 insertions(+) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index babb68c..087a76e 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,11 +2,17 @@ import java.io.FileNotFoundException; import java.io.FileReader; +import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; public class WC { + private Iterator si; + private String file = WC.class.getResource("someTextFile.txt").getFile(); + + // Nullary constructor for testing + public WC(){} public WC(String fileName) { try { @@ -20,4 +26,24 @@ public WC(String fileName) { public WC(Iterator si) { this.si = si; } + + public void logic() { + HashMap wordsAndAmounts = new HashMap(); + while (!(file.equals(null))) { + String[] arrayOfWords = file.split(" "); + // Ths for loop makes all words lower case for better counting and removes periods from + for (int j = 0; j < arrayOfWords.length; j++) { + arrayOfWords[j].toLowerCase(); + if (arrayOfWords[j].charAt(arrayOfWords[j].length()-1) == '.') { + arrayOfWords[j] = arrayOfWords[j].substring(0, arrayOfWords[j].length()-1); + } + } + for (int i = 0; i < wordsAndAmounts.size(); i++) { + if (wordsAndAmounts.containsKey()) { + + } + } + } + } + } diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt index e69de29..661e393 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -0,0 +1,3 @@ +Here is some text. +There will be words that are repeated. +This is one of them. \ No newline at end of file diff --git a/src/test/java/io/zipcoder/WCTest.java b/src/test/java/io/zipcoder/WCTest.java index 895e831..8c74a7e 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -8,4 +8,10 @@ public class WCTest { + @Test + public void printWC() { + WC wc = new WC(); + wc.logic(); + } + } \ No newline at end of file From 57b410d52abaf3110e34e548b2b2e8e7db871a90 Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Mon, 12 Mar 2018 15:55:56 -0400 Subject: [PATCH 05/12] Logic made, next comes printing --- src/main/java/io/zipcoder/WC.java | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 087a76e..1199f58 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -29,21 +29,21 @@ public WC(Iterator si) { public void logic() { HashMap wordsAndAmounts = new HashMap(); - while (!(file.equals(null))) { - String[] arrayOfWords = file.split(" "); - // Ths for loop makes all words lower case for better counting and removes periods from - for (int j = 0; j < arrayOfWords.length; j++) { - arrayOfWords[j].toLowerCase(); - if (arrayOfWords[j].charAt(arrayOfWords[j].length()-1) == '.') { - arrayOfWords[j] = arrayOfWords[j].substring(0, arrayOfWords[j].length()-1); - } + String[] arrayOfWords = file.split(" "); + // Ths for loop makes all words lower case for better counting and removes periods from the ends of any words that have them + for (int j = 0; j < arrayOfWords.length; j++) { + String currentWord = arrayOfWords[j]; + currentWord.toLowerCase(); + if (currentWord.charAt(arrayOfWords[j].length()-1) == '.') { + currentWord = arrayOfWords[j].substring(0, currentWord.length()-1); } for (int i = 0; i < wordsAndAmounts.size(); i++) { - if (wordsAndAmounts.containsKey()) { - - } + int count = wordsAndAmounts.containsKey(currentWord) ? wordsAndAmounts.get(currentWord) : 0; + wordsAndAmounts.put(currentWord, count + 1); } } + // Print out all words next to how many times they appear + } } From 46760f13b8d2894d48194032032421439ad76f91 Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Mon, 12 Mar 2018 16:19:42 -0400 Subject: [PATCH 06/12] what hell hath I wrought --- src/main/java/io/zipcoder/WC.java | 19 ++++++++++++++----- src/main/resources/someTextFile.txt | 3 ++- src/test/java/io/zipcoder/WCTest.java | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 1199f58..83f84fd 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -9,7 +9,7 @@ public class WC { private Iterator si; - private String file = WC.class.getResource("someTextFile.txt").getFile(); + private String file;// = WC.class.getResource("someTextFile.txt").getFile(); // Nullary constructor for testing public WC(){} @@ -23,14 +23,15 @@ public WC(String fileName) { } } - public WC(Iterator si) { - this.si = si; - } + // NOTED OUT BECAUSE STRING TESTS ARE FOR WEE BABIES +// public WC(Iterator si) { +// this.si = si; +// } public void logic() { + file = si.next(); HashMap wordsAndAmounts = new HashMap(); String[] arrayOfWords = file.split(" "); - // Ths for loop makes all words lower case for better counting and removes periods from the ends of any words that have them for (int j = 0; j < arrayOfWords.length; j++) { String currentWord = arrayOfWords[j]; currentWord.toLowerCase(); @@ -43,7 +44,15 @@ public void logic() { } } // Print out all words next to how many times they appear + for (String name: wordsAndAmounts.keySet()){ + String value = wordsAndAmounts.get(name).toString(); + System.out.println(name + " " + value); + } + } + public static void main(String[] args) { + WC wc = new WC(WC.class.getResource("/someTextFile.txt").getFile()); + wc.logic(); } } diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt index 661e393..e53251b 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -1,3 +1,4 @@ Here is some text. There will be words that are repeated. -This is one of them. \ No newline at end of file +This is one of them. +This lab was fun but now I do not know what to do. \ No newline at end of file diff --git a/src/test/java/io/zipcoder/WCTest.java b/src/test/java/io/zipcoder/WCTest.java index 8c74a7e..063e0ea 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -10,7 +10,7 @@ public class WCTest { @Test public void printWC() { - WC wc = new WC(); + WC wc = new WC(WC.class.getResource("/someTextFile.txt").getFile()); wc.logic(); } From 0cc439c00492f2cf6145c0a119dca966308ae472 Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Mon, 12 Mar 2018 17:35:43 -0400 Subject: [PATCH 07/12] Slightly less trash --- src/main/java/io/zipcoder/WC.java | 11 ++++++++--- src/main/resources/someTextFile.txt | 5 +---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 83f84fd..4eaa0cd 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -1,5 +1,6 @@ package io.zipcoder; +import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.HashMap; @@ -10,13 +11,15 @@ public class WC { private Iterator si; private String file;// = WC.class.getResource("someTextFile.txt").getFile(); + private String entireFile; // Nullary constructor for testing public WC(){} public WC(String fileName) { try { - this.si = new Scanner(new FileReader(fileName)); + //this.si = new Scanner(new FileReader(fileName)); + this.entireFile = new Scanner(new File(WC.class.getResource("/someTextFile.txt").getFile())).useDelimiter("\\A").next(); } catch (FileNotFoundException e) { System.out.println(fileName + " Does Not Exist"); System.exit(-1); @@ -29,13 +32,14 @@ public WC(String fileName) { // } public void logic() { - file = si.next(); + System.out.println(entireFile); + file = entireFile; HashMap wordsAndAmounts = new HashMap(); String[] arrayOfWords = file.split(" "); for (int j = 0; j < arrayOfWords.length; j++) { String currentWord = arrayOfWords[j]; currentWord.toLowerCase(); - if (currentWord.charAt(arrayOfWords[j].length()-1) == '.') { + if (currentWord.charAt(currentWord.length()-1) == '.') { currentWord = arrayOfWords[j].substring(0, currentWord.length()-1); } for (int i = 0; i < wordsAndAmounts.size(); i++) { @@ -50,6 +54,7 @@ public void logic() { } } + // psvm temporarily made for testing purposes public static void main(String[] args) { WC wc = new WC(WC.class.getResource("/someTextFile.txt").getFile()); wc.logic(); diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt index e53251b..bc32e82 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -1,4 +1 @@ -Here is some text. -There will be words that are repeated. -This is one of them. -This lab was fun but now I do not know what to do. \ No newline at end of file +Here is some text. There will be words that are repeated. This is one of them. This lab was fun but now I do not know what to do. \ No newline at end of file From d4646759b5f2293b89ab5a19e8c467feb4bca41c Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Mon, 12 Mar 2018 17:36:43 -0400 Subject: [PATCH 08/12] sout adding for debugging --- src/main/java/io/zipcoder/WC.java | 14 +++++++------- src/test/java/io/zipcoder/WCTest.java | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 4eaa0cd..83b3dc9 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,7 +2,7 @@ import java.io.File; import java.io.FileNotFoundException; -import java.io.FileReader; +import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; @@ -10,14 +10,14 @@ public class WC { private Iterator si; - private String file;// = WC.class.getResource("someTextFile.txt").getFile(); - private String entireFile; + private String entireFile;// = WC.class.getResource("someTextFile.txt").getFile(); // Nullary constructor for testing public WC(){} public WC(String fileName) { try { + // NOTED OUT TEMPORARILY BECAUSE AM DINGUS //this.si = new Scanner(new FileReader(fileName)); this.entireFile = new Scanner(new File(WC.class.getResource("/someTextFile.txt").getFile())).useDelimiter("\\A").next(); } catch (FileNotFoundException e) { @@ -26,16 +26,16 @@ public WC(String fileName) { } } - // NOTED OUT BECAUSE STRING TESTS ARE FOR WEE BABIES + // NOTED OUT BECAUSE STRING ITERATOR IS BLEH // public WC(Iterator si) { // this.si = si; // } public void logic() { System.out.println(entireFile); - file = entireFile; HashMap wordsAndAmounts = new HashMap(); - String[] arrayOfWords = file.split(" "); + String[] arrayOfWords = entireFile.split(" "); + System.out.println(Arrays.toString(arrayOfWords)); for (int j = 0; j < arrayOfWords.length; j++) { String currentWord = arrayOfWords[j]; currentWord.toLowerCase(); @@ -48,7 +48,7 @@ public void logic() { } } // Print out all words next to how many times they appear - for (String name: wordsAndAmounts.keySet()){ + for (String name : wordsAndAmounts.keySet()){ String value = wordsAndAmounts.get(name).toString(); System.out.println(name + " " + value); } diff --git a/src/test/java/io/zipcoder/WCTest.java b/src/test/java/io/zipcoder/WCTest.java index 063e0ea..1e92e1d 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -8,6 +8,7 @@ public class WCTest { + // What am I testing @Test public void printWC() { WC wc = new WC(WC.class.getResource("/someTextFile.txt").getFile()); From 507e822868e6da74d6111afd7958ac95df7aabe9 Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Mon, 12 Mar 2018 17:53:53 -0400 Subject: [PATCH 09/12] I'm a genius/idiot (for loop removed) --- src/main/java/io/zipcoder/WC.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 83b3dc9..c256b6e 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -32,23 +32,19 @@ public WC(String fileName) { // } public void logic() { - System.out.println(entireFile); HashMap wordsAndAmounts = new HashMap(); String[] arrayOfWords = entireFile.split(" "); - System.out.println(Arrays.toString(arrayOfWords)); for (int j = 0; j < arrayOfWords.length; j++) { String currentWord = arrayOfWords[j]; - currentWord.toLowerCase(); + currentWord = currentWord.toLowerCase(); if (currentWord.charAt(currentWord.length()-1) == '.') { currentWord = arrayOfWords[j].substring(0, currentWord.length()-1); } - for (int i = 0; i < wordsAndAmounts.size(); i++) { - int count = wordsAndAmounts.containsKey(currentWord) ? wordsAndAmounts.get(currentWord) : 0; - wordsAndAmounts.put(currentWord, count + 1); - } + int count = wordsAndAmounts.containsKey(currentWord) ? wordsAndAmounts.get(currentWord) : 0; + wordsAndAmounts.put(currentWord, count + 1); } // Print out all words next to how many times they appear - for (String name : wordsAndAmounts.keySet()){ + for (String name : wordsAndAmounts.keySet()){ String value = wordsAndAmounts.get(name).toString(); System.out.println(name + " " + value); } From 6c60621d174fb81979d9032b9c1487ca28ef3aec Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Tue, 13 Mar 2018 08:00:47 -0400 Subject: [PATCH 10/12] Word cleaner and counter --- src/main/java/io/zipcoder/WC.java | 21 +++++++-------------- src/main/resources/someTextFile.txt | 5 ++++- src/test/java/io/zipcoder/WCTest.java | 5 +---- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index c256b6e..a56a5d6 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,14 +2,13 @@ import java.io.File; import java.io.FileNotFoundException; -import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; public class WC { - private Iterator si; + // private Iterator si; private String entireFile;// = WC.class.getResource("someTextFile.txt").getFile(); // Nullary constructor for testing @@ -26,20 +25,16 @@ public WC(String fileName) { } } - // NOTED OUT BECAUSE STRING ITERATOR IS BLEH // public WC(Iterator si) { // this.si = si; // } public void logic() { HashMap wordsAndAmounts = new HashMap(); - String[] arrayOfWords = entireFile.split(" "); - for (int j = 0; j < arrayOfWords.length; j++) { - String currentWord = arrayOfWords[j]; - currentWord = currentWord.toLowerCase(); - if (currentWord.charAt(currentWord.length()-1) == '.') { - currentWord = arrayOfWords[j].substring(0, currentWord.length()-1); - } + String[] arrayOfWords = entireFile.split("\\s+"); + for (String arrayOfWord : arrayOfWords) { + String currentWord = arrayOfWord; + currentWord = wordCleaner(currentWord); int count = wordsAndAmounts.containsKey(currentWord) ? wordsAndAmounts.get(currentWord) : 0; wordsAndAmounts.put(currentWord, count + 1); } @@ -50,10 +45,8 @@ public void logic() { } } - // psvm temporarily made for testing purposes - public static void main(String[] args) { - WC wc = new WC(WC.class.getResource("/someTextFile.txt").getFile()); - wc.logic(); + private String wordCleaner(String word) { + return word.replaceAll("[^a-zA-Z ]", "").toLowerCase(); } } diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt index bc32e82..e53251b 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -1 +1,4 @@ -Here is some text. There will be words that are repeated. This is one of them. This lab was fun but now I do not know what to do. \ No newline at end of file +Here is some text. +There will be words that are repeated. +This is one of them. +This lab was fun but now I do not know what to do. \ No newline at end of file diff --git a/src/test/java/io/zipcoder/WCTest.java b/src/test/java/io/zipcoder/WCTest.java index 1e92e1d..37d06e6 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -3,12 +3,9 @@ import org.junit.Assert; import org.junit.Test; -import java.util.ArrayList; -import java.util.Arrays; - public class WCTest { - // What am I testing + // Bask in glory @Test public void printWC() { WC wc = new WC(WC.class.getResource("/someTextFile.txt").getFile()); From 60b9fc6136ab46e7bbcde4ca5765c4274e13fcec Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Tue, 13 Mar 2018 08:14:35 -0400 Subject: [PATCH 11/12] ParenChecker refactored and logicChecker() fixed for quotes --- src/main/java/io/zipcoder/ParenChecker.java | 33 +++++++++++---------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index 32cdaae..922572d 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -6,40 +6,43 @@ public class ParenChecker { private Stack charStack; - public ParenChecker(Stack charStack) { + ParenChecker(Stack charStack) { this.charStack = charStack; } public boolean logicChecker() { - if ((logic('(', ')')) && + return (logic('(', ')')) && logic('{', '}') && logic('[', ']') && logic('<', '>') && - logic('"', '"') && - logic('\'', '\'')) { - return true; - } - return false; + quoteCounter('\"') && + quoteCounter('\''); } - public boolean logic(char firstChar, char secondChar) { + private boolean logic(char firstChar, char secondChar) { int parenCount = 0; - for (int i = 0; i < charStack.size(); i++) { + for (Object aCharStack : charStack) { if (parenCount < 0) { return false; } - if (charStack.get(i).equals(firstChar)) { + if (aCharStack.equals(firstChar)) { parenCount++; } - if (charStack.get(i).equals(secondChar)) { + if (aCharStack.equals(secondChar)) { parenCount--; } } - if (parenCount != 0) { - return false; - } else { - return true; + return parenCount == 0; + } + + public boolean quoteCounter(char quote) { + int quoteCount = 0; + for (Object aCharStack : charStack) { + if (aCharStack.equals(quote)) { + quoteCount++; + } } + return quoteCount % 2 == 0; } } From 24d5cd00f97844c04204ad502b75ef48c2b857ae Mon Sep 17 00:00:00 2001 From: Mitch Taylor Date: Wed, 14 Mar 2018 10:37:54 -0400 Subject: [PATCH 12/12] Frankie is better at testing than I am --- src/test/java/io/zipcoder/WCTest.java | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/java/io/zipcoder/WCTest.java b/src/test/java/io/zipcoder/WCTest.java index 37d06e6..70b66cf 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -3,13 +3,44 @@ import org.junit.Assert; import org.junit.Test; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + public class WCTest { // Bask in glory @Test public void printWC() { WC wc = new WC(WC.class.getResource("/someTextFile.txt").getFile()); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); wc.logic(); + Assert.assertEquals("here 1\n" + + "but 1\n" + + "some 1\n" + + "be 1\n" + + "do 2\n" + + "lab 1\n" + + "repeated 1\n" + + "that 1\n" + + "not 1\n" + + "are 1\n" + + "of 1\n" + + "now 1\n" + + "text 1\n" + + "will 1\n" + + "one 1\n" + + "words 1\n" + + "this 2\n" + + "was 1\n" + + "i 1\n" + + "is 2\n" + + "them 1\n" + + "what 1\n" + + "there 1\n" + + "know 1\n" + + "to 1\n" + + "fun 1\n", outputStream.toString()); } } \ No newline at end of file