Hey I've been working on an encryption program just for fun and I need to know a way to make my code easier to read or if there is a simpler way to do this. I tried using a switch but I couldn't get that to work and I don't want to do a big line of if statements for the whole entire alphabet.
Here's my code:
Here's my code:
package encryption;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please input a sentence to encrypt: ");
String input = in.nextLine();
Encryption myCrypt = new Encryption();
String encryptdInput = myCrypt.encrypt(input.toLowerCase());
System.out.println(encryptdInput);
System.out.print("Please input a sentence to decrypt: ");
String de = in.nextLine();
String decryptdInput = myCrypt.decrypt(de);
System.out.println(decryptdInput);
}
}
class Encryption{
public String encrypt(String s){
for (int i = 0; i<s.length(); i++){
if (s.contains("a")){
s = s.replace("a", "%3");
} else if (s.contains("b")){
s = s.replace("b", "!83&~");
} else if (s.contains("c")){
s = s.replace("c", ".2");
} else if (s.contains("d")){
s = s.replace("d", "!83&`");
}
}return s;
}
public String decrypt(String s){
for (int i = 0; i<s.length(); i++){
if (s.contains("%3")){
s = s.replace("%3", "a");
} else if (s.contains("!83&~")){
s = s.replace("!83&~", "b");
} else if (s.contains(".2")){
s = s.replace(".2", "c");
} else if (s.contains("!83&`")){
s = s.replace("!83&`", "d");
}
} return s;
}
}


