|
@@ -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
|
}
|