-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathHamletParser.java
More file actions
57 lines (46 loc) · 1.52 KB
/
HamletParser.java
File metadata and controls
57 lines (46 loc) · 1.52 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
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();
}
public String getHamletData(){
return hamletData;
}
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 changeHamletToLeon(){
Pattern pattern1 = Pattern.compile("Hamlet");
Matcher matcher1 = pattern1.matcher(loadFile());
String leon = matcher1.replaceAll("Leon");
matcher1.reset(leon);
matcher1.usePattern(Pattern.compile("Horatio"));
return matcher1.replaceAll("Tariq");
}
//alternative method
public String changeName(String hamletName, String zcwName){
Pattern pattern = Pattern.compile(hamletName);
Matcher matcher = pattern.matcher(loadFile());
return matcher.replaceAll(zcwName);
}
}