|
@@ -1,15 +1,15 @@
|
1
|
1
|
import java.io.File;
|
2
|
2
|
import java.io.IOException;
|
3
|
|
-import java.util.ArrayList;
|
4
|
3
|
import java.util.Scanner;
|
5
|
|
-
|
|
4
|
+import java.lang.*;
|
|
5
|
+import java.util.*;
|
6
|
6
|
/**
|
7
|
7
|
* Created by thook on 10/7/15.
|
8
|
8
|
*/
|
9
|
9
|
public class HamletParser {
|
10
|
10
|
|
11
|
11
|
private String hamletData;
|
12
|
|
- String[] wordArr;
|
|
12
|
+ private Formatter formatter;
|
13
|
13
|
|
14
|
14
|
public HamletParser(){
|
15
|
15
|
this.hamletData = loadFile();
|
|
@@ -19,24 +19,33 @@ public class HamletParser {
|
19
|
19
|
ClassLoader classLoader = getClass().getClassLoader();
|
20
|
20
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
21
|
21
|
StringBuilder result = new StringBuilder("");
|
|
22
|
+ StringBuilder lineResult;
|
|
23
|
+ String[] wordArr;
|
22
|
24
|
|
23
|
25
|
try(Scanner scanner = new Scanner(file)){
|
24
|
26
|
while(scanner.hasNextLine()){
|
25
|
27
|
String line = scanner.nextLine();
|
26
|
|
- if (isHamletOrHoratio(line)){
|
27
|
|
- wordArr = line.split(" ");
|
|
28
|
+ wordArr = line.split(" ");
|
|
29
|
+ lineResult = new StringBuilder("");
|
|
30
|
+ for (String s: wordArr) {
|
|
31
|
+ if (findHamlet(s)) {
|
|
32
|
+ lineResult.append(changeHamletToLeon()).append(" ");
|
|
33
|
+ } else if (findHoratio(s)) {
|
|
34
|
+ lineResult.append(changeChangeHoratioToTariq()).append(" ");
|
|
35
|
+ } else {
|
|
36
|
+ lineResult.append(s).append(" ");
|
|
37
|
+ }
|
28
|
38
|
}
|
29
|
|
- //loop through line for next
|
30
|
|
- //if next matches Hamlet, replace with Leon
|
31
|
|
- //if next matches Horatio, replace with Tariq
|
32
|
|
- result.append(line).append("\n");
|
|
39
|
+ result.append(lineResult.toString().trim());
|
33
|
40
|
}
|
34
|
41
|
|
35
|
42
|
scanner.close();
|
36
|
43
|
}catch(IOException e){
|
37
|
44
|
e.printStackTrace();
|
38
|
45
|
}
|
39
|
|
-
|
|
46
|
+ openOutFile();
|
|
47
|
+ addText(result.toString());
|
|
48
|
+ closeOutFile();
|
40
|
49
|
return result.toString();
|
41
|
50
|
}
|
42
|
51
|
|
|
@@ -44,12 +53,49 @@ public class HamletParser {
|
44
|
53
|
return hamletData;
|
45
|
54
|
}
|
46
|
55
|
|
47
|
|
- public boolean isHamletOrHoratio(String line){
|
48
|
|
- if (line.toLowerCase().matches("hamlet") || line.toLowerCase().matches("horatio")){
|
|
56
|
+ public boolean findHamlet(String s){
|
|
57
|
+ if (s.toLowerCase().matches("hamlet")){
|
|
58
|
+ return true;
|
|
59
|
+ } else {
|
|
60
|
+ return false;
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ public boolean findHoratio(String s){
|
|
65
|
+ if (s.toLowerCase().matches("horatio")){
|
49
|
66
|
return true;
|
50
|
67
|
} else {
|
51
|
68
|
return false;
|
52
|
69
|
}
|
53
|
70
|
}
|
54
|
71
|
|
|
72
|
+ public String changeHamletToLeon(){
|
|
73
|
+ return "Leon";
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ public String changeChangeHoratioToTariq(){
|
|
77
|
+ return "Tariq";
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ /**
|
|
81
|
+ * Methods to print out to a new file
|
|
82
|
+ */
|
|
83
|
+ public void openOutFile(){
|
|
84
|
+ try {
|
|
85
|
+ formatter = new Formatter("output.txt");
|
|
86
|
+ }
|
|
87
|
+ catch (Exception e){
|
|
88
|
+ System.out.println("Error writing to file.");
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ public String addText(String s){
|
|
93
|
+ formatter.format("%s", s);
|
|
94
|
+ return loadFile();
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ public void closeOutFile(){
|
|
98
|
+ formatter.close();
|
|
99
|
+ }
|
|
100
|
+
|
55
|
101
|
}
|