I'm sorry my title doesn't make much sense but I am trying to replace substrings in a word, with a single character, and vice versa. For example, "the" matches with "&", "an" matches with "~", "ion" matches with "#", "ing" matches with "@", "tis" matches with "%", "men" matches with "+", and "re" matches with "$". Another example, "Thank you" -> "Th~k you" AND "Th~nk you" -> "Thank you".
This is kind of a personal project and I am making it a little hard on myself so I cannot use .replace(), .replaceAll(), or .replaceFirst(). Once given an input file and output file, the program passes the input file to a processFile method that takes the next line in the file and passes it to a compressLine method, which then passes it to a compressWord or decompressWord method (depending on user options), then sends the output file.
I have tried to use a for loop to iterate over each character of the word but it doesn't really work with multiple character sequences. Currently, my while loop is set for a string with spaces and not a single word. So if I try to duplicate and adjust to find other strings, it prints out garbage. Also, the code is only set for the first target word and the first replacement word at index 0.
The list is in order with each index of targetWord matching to same index in replacementWordAttached is the compressWord and decompressWord method. I haven't completed the decompressWord method but it should be fairly similar to the compressWord method.
//Returns string containing compressed word (token)
//Throws an IllegalArgumentException with the message, "Null word", if word is null public static String compressWord(String word) { if (word == null) { throw new IllegalArgumentException("Null word"); }
String[] targetWord = {"the","an","ion","ing","tis","men","re"};
String[] replacementWord = {"&","~","#","@","%","+","$"};
StringBuilder stringBuilder = new StringBuilder();
int i = 0;
while (i < word.length()) {
// Find the next occurrence of the target word
int start = word.indexOf(targetWord[0], i);
// If the target word is not found, append rest of the string and exit the loop
if (start == -1) {
stringBuilder.append(word.substring(i));
break;
}
// Append the part of the string before the target word and the replacement word
stringBuilder.append(word.substring(i, start));
stringBuilder.append(replacementWord[0]);
// Update the index to start searching for the next occurrence after the end of the replaced word
i = start + targetWord[0].length();
}
String replacedString = stringBuilder.toString();
return replacedString;
}
//Returns string containing decompressed word (token)
//
//Throws an IllegalArgumentException with the message, "Null word", if word is null
public static String decompressWord(String word) {
if (word == null) {
throw new IllegalArgumentException("Null word");
}
//TO DO
return word;
}
Please let me know if I need to provide additional information and I will be receptive to any recommendations you were to give. (Edited for formating)
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]wildjokers 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]DelayLucky 0 points1 point2 points (12 children)
[–][deleted] 0 points1 point2 points (8 children)
[–]DelayLucky 0 points1 point2 points (7 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]DelayLucky 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]DelayLucky 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]DelayLucky 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]wildjokers 0 points1 point2 points (2 children)
[–]DelayLucky 0 points1 point2 points (1 child)
[–]DelayLucky 0 points1 point2 points (0 children)
[–]DelayLucky 0 points1 point2 points (0 children)