here is the code
import java.util.Scanner;
public class Cipher {
public static void main (String args[]) {
int key = 0;
String sentence;
Scanner input = new Scanner(System.in);
//declare, create, initialize alphabet array
char[] anArrayOfLetters = {'a','b','c','d','e','f','g','h','i','j','k','l',' m','n','o','p','q','r','s','t','u','v','w','x','y' ,'z'};
//get sentence and key from user
System.out.println("Enter a sentence to encrypt: ");
sentence = input.nextLine();
System.out.println("Enter an integer to use as the encryption key: ");
key = input.nextInt();
int element=0;
boolean notLetter=false;
//find each letter and encrypt it using the key
for (int i =0; i < sentence.length(); i++) {
//check if first character is in arrayOfLetters
for (int j = 0; j < anArrayOfLetters.length; j++ ) {
if (sentence.charAt(i) == anArrayOfLetters[j]){
notLetter = false;
if ((j+ key) > 25) {
element = (j+key)%25; //finds the letter when the end of alphabet is surpassed
}
else
element = j + key;
}
else
notLetter = true;
}//end for
if (notLetter == false)
sentence.replace(sentence.charAt(i), anArrayOfLetters[element]);
}//end for
System.out.println("The encrypted sentence is : " + sentence);
//de-encrypt the sentence
for (int a =0; a < sentence.length(); a++) {
//check if first character is in arrayOfLetters
for (int b = 0; b < anArrayOfLetters.length; b++ ) {
if (sentence.charAt(a) == anArrayOfLetters[b]){
notLetter = false;
if ((b- key) < 0)
element = (b-key) + 25; //finds the letter when the beginning of alphabet is surpassed
else
element = b - key;
}
else
notLetter = true;
}//end for
if (notLetter == false)
sentence.replace(sentence.charAt(a), anArrayOfLetters[element]); }//end for
System.out.println(sentence);
}
}
I am not sure if the line in red is correct and in the right place.
the output is the same as the input with no encryption...