Reputation: 15
I'm doing an encryption app with 4 different methods of encryption, one of them is a vowel replace, the problem is that is not working and I'm not sure why,
this is the vowelReplace class:
@Override
public String encrypt(String phraseToEncrypt) {
phraseToEncrypt.replace('a','e');
phraseToEncrypt.replace('e','i');
phraseToEncrypt.replace('i','o');
phraseToEncrypt.replace('o','u');
phraseToEncrypt.replace('u','a');
return phraseToEncrypt;
}
@Override
public String decrypt(String phraseToDecrypt) {
String oldStr = phraseToDecrypt;
oldStr.replace('a','u');
oldStr.replace('e','a');
oldStr.replace('i','e');
oldStr.replace('o','i');
oldStr.replace('u','o');
return oldStr;
}
}
this is my interface:
public interface StringEncrypter {
String encrypt(String phraseToEncrypt);
String decrypt (String phraseToDecrypt);
}
and the controller where I handle the input and selection of the encrypters:
public class EncrypterController {
private ConsoleIO consoleIO = new ConsoleIO();
private EncrypterUI ui = new EncrypterUI();
private final List<StringEncrypter> encryptors = new ArrayList<>();
private String encryptedMessage = null;
public void run() throws IOException {
while(true) {
int selection = ui.promptMainMenu();
switch (selection) {
case 1:
pickEncrypter();
break;
case 2:
break;
case 3:
break;
case 4:
String message = consoleIO.promptForString("Type your message to encrypt");
if(!encryptors.isEmpty()){
for(int i=0; i < encryptors.size() ;i++){
message = encryptors.get(i).encrypt(message);
}
encryptedMessage = message;
System.out.println(encryptedMessage);
}
break;
case 5:
if(encryptedMessage != null && !encryptors.isEmpty()){
for(int i=encryptors.size()-1; i>=0 ;i--){
encryptedMessage = encryptors.get(i).decrypt(encryptedMessage);
}
System.out.println(encryptedMessage);
}
break;
case 0:
return;
}
}
}
private void pickEncrypter() throws IOException {
System.out.println("1 - Doubler\n" +
"2 - Cutter\n" +
"3 - VowelReplacer");
int sel = consoleIO.promptForInt(1, 3);
switch(sel) {
case 1:
System.out.println("Doubler");
Doubler dd = new Doubler();
encryptors.add(dd);
break;
case 2:
System.out.println("Cutter");
Cutter cc = new Cutter();
encryptors.add(cc);
break;
case 3:
System.out.println("Vowel Replacer");
VowelReplacer vv = new VowelReplacer();
encryptors.add(vv);
break;
}
}
}
Upvotes: 0
Views: 414
Reputation: 6307
You have two issues here, the first is that you need to You need to assign the adjusted string back to itself otherwise nothing changes. You can do this like this:
phraseToEncrypt = phraseToEncrypt.replace('a','e');
The second issue is that you are replacing characters more than once. You can solve this using a loop as shown in the other answer which is the more correct method, however, you can also use a placeholder character for example �
to ensure that no letters are doubled up or changed more than once.
Be careful though because if the placeholder character exists in the string before encrypting then things will break. The placeholder method could look something like this:
public String encrypt(String phraseToEncrypt){
//Set 'a' to be our placeholder value `�` which is `\u265C` in unicode:
phraseToEncrypt = phraseToEncrypt.replace('a', '\u265C');
//change the order of replacing characters
//so that you don't change a character more than once
phraseToEncrypt = phraseToEncrypt.replace('u', 'a');
phraseToEncrypt = phraseToEncrypt.replace('o', 'u');
phraseToEncrypt = phraseToEncrypt.replace('i', 'o');
phraseToEncrypt = phraseToEncrypt.replace('e', 'i');
//finally we can now change the placeholder back to the newly freed up `e` value
phraseToEncrypt = phraseToEncrypt.replace('\u265C', 'e');
return phraseToEncrypt;
}
An input of This is a test message with vowels
would return the following string:
`Thos os e tist missegi woth vuwils`
The decrypt method would be the same using a place holder character, and changing the letters in the correct order so that nothing is changed more than once.
Upvotes: 0
Reputation: 856
As mentioned in the comments above, String.replace()
returns a new string. You can just assign the output to the same variable below.
@Override
public String encrypt(String phraseToEncrypt) {
phraseToEncrypt = phraseToEncrypt.replace('a','e');
phraseToEncrypt = phraseToEncrypt.replace('e','i');
phraseToEncrypt = phraseToEncrypt.replace('i','o');
phraseToEncrypt = phraseToEncrypt.replace('o','u');
phraseToEncrypt = phraseToEncrypt.replace('u','a');
return phraseToEncrypt;
}
Also mentioned was the fact that if you replace all the 'a' with 'e' and then replace all the 'e' with 'i', all of the chars that were originally 'a' will wind up as 'i' instead of 'e'. You could solve that as follows.
@Override
public String encrypt(String phraseToEncrypt) {
StringBuilder encryptedString = new StringBuilder();
char[] chars = phraseToEncrypt.toCharArray();
for (char ch : chars) {
switch (ch) {
case 'a' : encryptedString.append('e'); break;
case 'e' : encryptedString.append('i'); break;
case 'i' : encryptedString.append('o'); break;
case 'o' : encryptedString.append('u'); break;
case 'u' : encryptedString.append('a'); break;
default : encryptedString.append(ch);
}
}
return encryptedString.toString();
}
Upvotes: 1