-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathHamletParser.java
More file actions
81 lines (66 loc) · 2.04 KB
/
HamletParser.java
File metadata and controls
81 lines (66 loc) · 2.04 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
76
77
78
79
80
81
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Formatter;
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 boolean findHamlet() {
if (hamletData.contains("Hamlet")) {
return true;
}
return false;
}
public boolean findHoratio() {
if (hamletData.contains("Horatio")) {
return true;
}
return false;
}
public void changeHamletToLeon() {
Matcher matcher = matchHamlet();
hamletData = matcher.replaceAll("Leon");
}
public void changeHoratioToTariq() {
Matcher matcher = matchHoratio();
hamletData = matcher.replaceAll("Tariq");
}
public Matcher matchHamlet() {
return Pattern.compile("(?i)Hamlet").matcher(hamletData);
}
public Matcher matchHoratio() {
return Pattern.compile("(?i)Horatio").matcher(hamletData);
}
public void changeFile() throws IOException {
FileWriter fileWriter = new FileWriter("/Users/zach/dev/Maven.Regex-Hamlet-Parser/src/main/resources/hamlet.txt");
changeHoratioToTariq();
changeHamletToLeon();
fileWriter.write(hamletData);
fileWriter.close();
}
}