|
@@ -1,6 +1,8 @@
|
1
|
1
|
import java.io.File;
|
2
|
2
|
import java.io.IOException;
|
3
|
3
|
import java.util.Scanner;
|
|
4
|
+import java.util.regex.Matcher;
|
|
5
|
+import java.util.regex.Pattern;
|
4
|
6
|
|
5
|
7
|
/**
|
6
|
8
|
* Created by thook on 10/7/15.
|
|
@@ -9,31 +11,61 @@ public class HamletParser {
|
9
|
11
|
|
10
|
12
|
private String hamletData;
|
11
|
13
|
|
12
|
|
- public HamletParser(){
|
|
14
|
+ public HamletParser() {
|
13
|
15
|
this.hamletData = loadFile();
|
14
|
16
|
}
|
15
|
17
|
|
16
|
|
- private String loadFile(){
|
|
18
|
+ private String loadFile() {
|
17
|
19
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
20
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
21
|
StringBuilder result = new StringBuilder("");
|
20
|
22
|
|
21
|
|
- try(Scanner scanner = new Scanner(file)){
|
22
|
|
- while(scanner.hasNextLine()){
|
|
23
|
+ try (Scanner scanner = new Scanner(file)) {
|
|
24
|
+ while (scanner.hasNextLine()) {
|
23
|
25
|
String line = scanner.nextLine();
|
24
|
26
|
result.append(line).append("\n");
|
25
|
27
|
}
|
26
|
28
|
|
27
|
29
|
scanner.close();
|
28
|
|
- }catch(IOException e){
|
|
30
|
+ } catch (IOException e) {
|
29
|
31
|
e.printStackTrace();
|
30
|
32
|
}
|
31
|
|
-
|
32
|
33
|
return result.toString();
|
33
|
34
|
}
|
34
|
35
|
|
35
|
|
- public String getHamletData(){
|
|
36
|
+ public String getHamletData() {
|
36
|
37
|
return hamletData;
|
37
|
38
|
}
|
38
|
39
|
|
|
40
|
+
|
|
41
|
+ public void changeIfHamlet() {
|
|
42
|
+ Pattern p = Pattern.compile("(Hamlet| HAMLET).*");
|
|
43
|
+ Matcher m = p.matcher(hamletData);
|
|
44
|
+ if (m.find()) {
|
|
45
|
+ hamletData = m.replaceAll("Leon");
|
|
46
|
+ } else if (m.find()){
|
|
47
|
+ hamletData = m.replaceAll("LEON");
|
|
48
|
+ }
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ public void changeIfHoratio(){
|
|
52
|
+ Pattern p = Pattern.compile("(Horatio | HORATIO).*");
|
|
53
|
+ Matcher m = p.matcher(hamletData);
|
|
54
|
+ if (m.find()){
|
|
55
|
+ hamletData = m.replaceAll("Tariq");
|
|
56
|
+ } else if (m.find()) {
|
|
57
|
+ hamletData = m.replaceAll("TARIQ");
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+ public boolean findHamlet(){
|
|
61
|
+ Pattern p = Pattern.compile("(Hamlet| HAMLET).*");
|
|
62
|
+ Matcher m = p.matcher(hamletData);
|
|
63
|
+ return m.find();
|
|
64
|
+ }
|
|
65
|
+ public boolean findHoratio(){
|
|
66
|
+ Pattern p = Pattern.compile("(Horatio | HORATIO).*");
|
|
67
|
+ Matcher m = p.matcher(hamletData);
|
|
68
|
+ return m.find();
|
|
69
|
+ }
|
39
|
70
|
}
|
|
71
|
+
|