Skip to content

how to replace the vowels from a String with other vowels for encryption?

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 - Doublern" +
            "2 - Cuttern" +
            "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;
    }
}

}

Answer

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();
}