Khalil Malik Saboor 6 years ago
parent
commit
e6bfc1ee36
2 changed files with 18 additions and 10 deletions
  1. 14
    6
      src/main/java/HamletParser.java
  2. 4
    4
      src/test/java/HamletParserTest.java

+ 14
- 6
src/main/java/HamletParser.java View File

@@ -39,14 +39,22 @@ public class HamletParser {
39 39
         return hamletData;
40 40
     }
41 41
 
42
-    public static String findHamlet(String findHamlet){
43
-
44
-        return null;
42
+    public static String findHamlet(String Sentence, String word){
43
+        StringBuilder sb = new StringBuilder();
44
+        String str = "";
45
+
46
+        Pattern checkRegex = Pattern.compile(word);
47
+        Matcher regexMatch = checkRegex.matcher(Sentence);
48
+        if (regexMatch.find()){
49
+            str = regexMatch.group();
50
+        } else {
51
+            return  null;
52
+        }
53
+        return str;
45 54
     }
46 55
 
47
-    public static String findHoratio(String findHoratio) {
48
-
49
-        return null;
56
+    public static String findHoratio(String Sentence, String Word) {
57
+            return findHamlet(Sentence, Word);
50 58
     }
51 59
 
52 60
 }

+ 4
- 4
src/test/java/HamletParserTest.java View File

@@ -26,19 +26,19 @@ public class HamletParserTest {
26 26
 
27 27
     @Test
28 28
     public void testFindHoratio() {
29
-        String findHoratio = "Horatio Kim James, King Author";
29
+        String Sentence = hamletText;
30 30
 
31 31
         String expected = "Horatio";
32
-        String actual = HamletParser.findHoratio(findHoratio);
32
+        String actual = HamletParser.findHoratio(Sentence, expected);
33 33
 
34 34
         Assert.assertEquals(expected,actual);
35 35
     }
36 36
     @Test
37 37
     public void testFindHamlet() {
38
-        String findHamlet = "Hamlet Kim James, King Author";
38
+        String Sentence = hamletText;
39 39
 
40 40
         String expected = "Hamlet";
41
-        String actual = HamletParser.findHamlet(findHamlet);
41
+        String actual = HamletParser.findHamlet(Sentence, expected);
42 42
 
43 43
         Assert.assertEquals(expected,actual);
44 44
     }