I'm trying to implement a simple cesar cipher. I'm having trouble with the function that codes a string based on a new key[]. Here I shown, how I setup my key.
private String[] alphabet = {"a", "b", "c","d", "e", "f","g", "h", "i","j", "k", "l","m", "n", "o",
"p", "q", "r","s", "t", "u","v", "w", "x","u", "z"};
public String[] CoderNewKey(int shift) {
_NewKey = null;
_NewKey = new String[25];
for (int i = 0; i < 25; i++) {
_NewKey[i] = alphabet[(i + shift) % 25];
}
return _NewKey;
}
The problem I have is with Coder, none of the if statements are ever true, and rtnstring is always just "".
public String Coder(String[] key, String msg){
String rtnstring = "";
for ( int i = 0; i < msg.length();i++){
for (int x = 0; x<26 ; x++){
if ( msg.substring(i, i+1) == alphabet[x]){
rtnstring.concat(key[x]);
}if (msg.substring(i, i+1) == " "){
rtnstring.concat(" ");
}
}
}
return rtnstring;
}
Here is my logic for the coder method.
It goes through the String msg 1 letter at a time. It matches the letter with its index in the alphabet, then it creates a new string based on the index's in the given key.