Needing this output:
public static void main(String[] args) {
Cipher cipher; // creating Cipher type variable
// printing welcome messsage on console
Scanner keyboard = new Scanner(System.in); // Scanner object for user input
System.out.println("Welcome to Nicholas Coleman's Cipher Program!\n");
String nextLine = keyboard.nextLine();
/*
put everything inside while loop
so that we can continue program as many time as user request for continue program
*/
while (true) {
/*
Prompt for User choice Encrypt or decrypt
then Prompt for Cipher ShiftCipher or VigenereCipher
and store user input in variable operation and whichCipher respectively
*/
System.out.print("Do you want to [E]ncrypt or [D]ecrypt? ");
String operation = keyboard.nextLine();
System.out.print("Do you want to use a [S]hift cipher or [V]igenere Cipher? ");
String whichCipher = keyboard.nextLine();
/*
check operation to be performed
*/
if(operation.equals("E")) { // User selected encryption
if(whichCipher.equals("S")) { // user selected ShiftCipher
/*
Prompt for appropriate input and create instance of ShiftCipher
then call encrypt() method to encrypt plaintext entered by user
*/
System.out.print("Pease enter a number between 0 and 25 to use as a key: ");
int key = keyboard.nextInt();
keyboard.nextLine();
System.out.print("Please enter the plaintext to be encrypted: ");
String plaintext = keyboard.nextLine();
cipher = new ShiftCipher(key);
System.out.println("The corresponding ciphertext is: " + cipher.encrypt(plaintext));
}
else if(whichCipher.equals("V")) { // user selected VigenereCipher
/*
Prompt for appropriate input and create instance of VigenereCipher
then call encrypt() method to encrypt plaintext entered by user
*/
System.out.print("Pease enter a keyword: ");
String keyword = keyboard.nextLine();
System.out.print("Please enter the plaintext to be encrypted: ");
String plaintext = keyboard.nextLine();
cipher = new VigenereCipher(keyword);
System.out.println("The corresponding ciphertext is: " + cipher.encrypt(plaintext));
}
}
else if (operation.equals("D")) { // user selected decrypt
if(whichCipher.equals("S")) { // user selected ShiftCipher
/*
Prompt for appropriate input and create instance of ShiftCipher
then call decrypt() method to decrypt ciphertext entered by user
*/
System.out.print("Pease enter a number between 0 and 25 to use as a key: ");
int key = keyboard.nextInt();
keyboard.nextLine();
System.out.print("Please enter the ciphertext to be decrypted: ");
String ciphertext = keyboard.nextLine();
cipher = new ShiftCipher(key);
System.out.println("The corresponding plaintext is: " + cipher.decrypt(ciphertext));
}
else if(whichCipher.equals("V")) { // user selected VigenereCipher
/*
Prompt for appropriate input and create instance of VigenereCipher
then call decrypt() method to decrypt ciphertext entered by user
*/
System.out.print("Pease enter a keyword: ");
String keyword = keyboard.nextLine();
System.out.print("Please enter the plaintext to be decrypted: ");
String ciphertext = keyboard.nextLine();
cipher = new VigenereCipher(keyword);
System.out.println("The corresponding ciphertext is: " + cipher.decrypt(ciphertext));
}
}
// Ask user if they want to continue
System.out.print("Do you want to continue(Y/N)? ");
String cont = keyboard.nextLine();
// if user enter Y then continue while loop
if(cont.equals("Y")) {
continue;
}
// if user enter N then display a then you message and terminate(break) while loop
else if(cont.equals("N")) {
System.out.println("Thank you for using Nicholas Coleman's cipher program.");
break;
}
}
}
}
Results after running:
--- exec-maven-plugin:1.5.0:exec (default-cli) @ ProgrammingAssignment2 --- Welcome to Nicholas Coleman's Cipher Program!
Do you want to [E]ncrypt or [D]ecrypt? Do you want to use a [S]hift cipher or [V]igenere Cipher? Do you want to continue(Y/N)? D
Note: All the user input is terminated by pressing enter. That allows no, one or several characters to be entered as one value. If you need to react to each and every keypress neither Scanner nor Console are your friends and you have to deal with System.in directly.
Print a line without asking for user input:
System.out.println("Hello World");
Print a prompt, then ask for user input:
Scanner s = new Scanner(System.in);
System.out.println("Enter User name:");
String username = s.nextLine();
Print a prompt and ask for user input on the same line:
Scanner s = new Scanner(System.in);
System.out.print("Enter User name: ");
String username = s.nextLine();
Print a prompt and ask for user input without echo:
Console c = System.console();
char[] password1 = c.readPassword("Enter password: ");
String password2 = new String(c.readPassword("Enter password: "));