|
@@ -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.
|
|
@@ -16,19 +18,16 @@ public class HamletParser {
|
16
|
18
|
private String loadFile(){
|
17
|
19
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
20
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
|
- StringBuilder result = new StringBuilder("");
|
|
21
|
+ StringBuilder result = new StringBuilder();
|
20
|
22
|
|
21
|
23
|
try(Scanner scanner = new Scanner(file)){
|
22
|
24
|
while(scanner.hasNextLine()){
|
23
|
25
|
String line = scanner.nextLine();
|
24
|
26
|
result.append(line).append("\n");
|
25
|
27
|
}
|
26
|
|
-
|
27
|
|
- scanner.close();
|
28
|
|
- }catch(IOException e){
|
|
28
|
+ } catch(IOException e){
|
29
|
29
|
e.printStackTrace();
|
30
|
30
|
}
|
31
|
|
-
|
32
|
31
|
return result.toString();
|
33
|
32
|
}
|
34
|
33
|
|
|
@@ -36,4 +35,25 @@ public class HamletParser {
|
36
|
35
|
return hamletData;
|
37
|
36
|
}
|
38
|
37
|
|
|
38
|
+ public void changeHamletToLeon() {
|
|
39
|
+ Pattern hamPattern = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
|
|
40
|
+ Matcher hamMatcher = hamPattern.matcher(hamletData);
|
|
41
|
+ hamletData = hamMatcher.replaceAll("Leon");
|
|
42
|
+ }
|
|
43
|
+ public void changeHoratioToTariq() {
|
|
44
|
+ Pattern horPattern = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
|
|
45
|
+ Matcher horMatcher = horPattern.matcher(hamletData);
|
|
46
|
+ hamletData = horMatcher.replaceAll("Tariq");
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ public boolean findHoratio() {
|
|
50
|
+ Pattern horPattern = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
|
|
51
|
+ Matcher horMatcher = horPattern.matcher(hamletData);
|
|
52
|
+ return horMatcher.find();
|
|
53
|
+ }
|
|
54
|
+ public boolean findHamlet() {
|
|
55
|
+ Pattern hamPattern = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
|
|
56
|
+ Matcher hamMatcher = hamPattern.matcher(hamletData);
|
|
57
|
+ return hamMatcher.find();
|
|
58
|
+ }
|
39
|
59
|
}
|