It would be easier if you used String matching with regular expressions.
First of all, in your loop condition, eliminate -1
. Otherwise you're only getting to the second to last character.
Next, eliminate the cryptText.append(currentChar);
before the if statement.
Then, use this
char currentChar = plainText.charAt(i);
String cS = currentChar+"";
currentChar = (char) ((char) (currentChar - (int)'A' + 13) % 26 + (int)'A');
if (!cS.matches("[A-Z]")) {
cryptText.append(plainText.charAt(i));
}
else {
cryptText.append(currentChar);
}
This actually works, except that I'm getting some different characters from what you want. HELLO WORLD! => URYYB JBEYQ!