-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathHamletParser.java
More file actions
75 lines (60 loc) · 2.03 KB
/
HamletParser.java
File metadata and controls
75 lines (60 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by thook on 10/7/15.
*/
public class HamletParser {
private String hamletData;
public HamletParser() {
this.hamletData = loadFile();
}
private String loadFile() {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("hamlet.txt").getFile());
StringBuilder result = new StringBuilder("");
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
result.append(line).append("\n");
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
public String getHamletData() {
return hamletData;
}
public String changeHamletToLeon(String input) {
Pattern smallCase = Pattern.compile("Hamlet");
Matcher m1 = smallCase.matcher(input);
String result1 = m1.replaceAll("Leon");
Pattern upperCase = Pattern.compile("HAMLET");
Matcher m2 = upperCase.matcher(result1);
String result2 = m2.replaceAll("LEON");
return result2;
}
public String changeHoratioToTariq(String input) {
Pattern smallCase = Pattern.compile("Horatio");
Matcher m1 = smallCase.matcher(input);
String result1 = m1.replaceAll("Tariq");
Pattern upperCase = Pattern.compile("HORATIO");
Matcher m2 = upperCase.matcher(result1);
String result2 = m2.replaceAll("TARIQ");
return result2;
}
public boolean findHamlet(String input) {
if (hamletData.contains("HAMLET") || hamletData.contains("Hamlet")) {
}
return true;
}
public boolean findHoratio(String input) {
if (hamletData.contains("HORATIO") || hamletData.contains("Horatio")) {
}
return true;
}
}