Skip to content

Hamlet Replacer #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,69 @@
import java.io.File;
import java.io.FileWriter;
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;
private static String hamletData;

public HamletParser(){
this.hamletData = loadFile();
HamletParser(){
hamletData = loadFile();
}

private String loadFile(){
public 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 static String changeHamletToLeon(String string) {
StringBuffer sb = new StringBuffer();
String hamletPattern = "([Hh][Aa][Mm][Ll][Ee][Tt])";
Pattern pattern = Pattern.compile(hamletPattern);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
matcher.appendReplacement(sb, "Leon");
}
return matcher.appendTail(sb).toString();
}

public static String changeHoratioToTariq(String string) {
StringBuffer sb = new StringBuffer();
String horatioPattern = "([Hh][Oo][Rr][Aa][Tt][Ii][Oo])";
Pattern pattern = Pattern.compile(horatioPattern);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
matcher.appendReplacement(sb, "Tariq");
}
return matcher.appendTail(sb).toString();
}

private void changeEverythingAndPutInFile() throws IOException {
String fixedFile = changeHamletToLeon(changeHoratioToTariq(hamletData));
FileWriter fileWriter = new FileWriter("src/main/resources/zipcodeHamlet.txt", true);
fileWriter.write(fixedFile);
fileWriter.close();
}

public static void main(String[] args) throws IOException {
HamletParser h = new HamletParser();
h.changeEverythingAndPutInFile();
}

}
Loading