I need to cycle through each character in a string, and based on the character that it is, replace it with another character from a char array.
Basically it looks like this:
for (int k = 0; k < messageToBeEncrypted.length(); k++)
{
switch(messageToBeEncrypted.charAt(k))
{
case 'a' : messageToBeEncrypted.replace('a', cryptList[0]);
break;
case 'b' : messageToBeEncrypted.replace('b', cryptList[1]);
break;
//it keeps going for each letter of the alphabet
}
System.out.println(messageToBeEncrypted);
}
The char array cryptList is a randomly generated alphabet, "fgtaixnebqwjkzumlydrovsphc" A is to be replaced by f, b by g, and so on. The problem I'm having is that this code prints the exact same message that was inputted, so if the messageToBeEncrypted was ab, instead of fg, it prints ab. How can I fix this? And if theres a clearer more concise way to accomplish this, do tell me. I realize 26 case statements probably isn't the best way to achieve my goal.