Browse Source

finished changeHamToLeon method

Luis Romero 6 years ago
parent
commit
eb13f29c46

+ 28
- 2
src/main/java/HamletParser.java View File

@@ -1,6 +1,9 @@
1 1
 import java.io.File;
2 2
 import java.io.IOException;
3
+import java.util.Arrays;
3 4
 import java.util.Scanner;
5
+import java.util.regex.Pattern;
6
+import java.util.regex.Matcher;
4 7
 
5 8
 /**
6 9
  * Created by thook on 10/7/15.
@@ -8,11 +11,18 @@ import java.util.Scanner;
8 11
 public class HamletParser {
9 12
 
10 13
     private String hamletData;
14
+    private String[] words;
11 15
 
12 16
     public HamletParser(){
13 17
         this.hamletData = loadFile();
14 18
     }
15 19
 
20
+    // My constructor
21
+    public HamletParser(String input) {
22
+        this.hamletData = input;
23
+        this.words = input.split("\\W");
24
+    }
25
+
16 26
     private String loadFile(){
17 27
         ClassLoader classLoader = getClass().getClassLoader();
18 28
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
@@ -23,9 +33,8 @@ public class HamletParser {
23 33
                 String line = scanner.nextLine();
24 34
                 result.append(line).append("\n");
25 35
             }
26
-
27 36
             scanner.close();
28
-        }catch(IOException e){
37
+        } catch(IOException e) {
29 38
             e.printStackTrace();
30 39
         }
31 40
 
@@ -36,4 +45,21 @@ public class HamletParser {
36 45
         return hamletData;
37 46
     }
38 47
 
48
+    public void changeHamletToLeon() {
49
+        String HamletString = "Hamlet";
50
+        String LeonString = "Leon";
51
+
52
+        Pattern HamletPattern = Pattern.compile(HamletString);
53
+        Matcher HamletMatcher = HamletPattern.matcher(hamletData);
54
+        hamletData = HamletMatcher.replaceAll(LeonString);
55
+    }
56
+
57
+    public static void main(String[] args) {
58
+        String inputString = "Change hamleta;ldsfjkhamletHamlet and Hamlet and also Hamlet to Leon";
59
+        HamletParser hamletParser = new HamletParser(inputString);
60
+        System.out.println(hamletParser.getHamletData());
61
+        hamletParser.changeHamletToLeon();
62
+        System.out.println(hamletParser.getHamletData());
63
+    }
64
+
39 65
 }

+ 5383
- 0
src/main/resources/hamletShort.txt
File diff suppressed because it is too large
View File


+ 10
- 0
src/test/java/HamletParserTest.java View File

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
@@ -15,6 +16,15 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        // Given
20
+        String inputString = "Change Hamlet to Leon";
21
+        String expectedString = "Change Leon to Leon";
22
+        hamletParser = new HamletParser(inputString);
23
+        // When
24
+        hamletParser.changeHamletToLeon();
25
+        String actualString = hamletParser.getHamletData();
26
+        // Then
27
+        Assert.assertEquals(expectedString, actualString);
18 28
     }
19 29
 
20 30
     @Test

+ 5383
- 0
target/classes/hamlet.txt
File diff suppressed because it is too large
View File


+ 5383
- 0
target/classes/hamletShort.txt
File diff suppressed because it is too large
View File