Question: public class NewCeaser { public static final String ALPHABET = abcdefghijklmnopqrstuvwxyz; //qwertyuiopasdfghjklzxcvbnm public static void main(String[] args) { String encrypt = , message = ,
public class NewCeaser {
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
//qwertyuiopasdfghjklzxcvbnm public static void main(String[] args) {
String encrypt = "", message = "", code;
String cipherText = new String();
/* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);
int choice;
/* * This while loop continues to execute until the user enters a valid * choice or 3 */ System.out.print("Enter the String of code: "); code = sc.next(); while (true) { // displaying the menu System.out.println("You may:"); System.out.println("\t1) Encrypt a message"); System.out.println("\t2) Decrypt a message"); System.out.println("\t3) Quit");
// getting the choice entered by the user System.out.print("Enter choice (1,2, or 3): "); choice = sc.nextInt();
// Based on the user choice the corresponding case will be executed switch (choice) { case 1: { sc.nextLine(); // Getting the input entered by the user System.out.print("Enter the message :"); code = sc.nextLine(); code = code.toLowerCase();
// Encrypting the message cipherText = cipherText.toLowerCase(); message = ""; for (int ii = 0; ii < cipherText.length(); ii++) { int charPosition = ALPHABET.indexOf(cipherText.charAt(ii)); char replaceVal = code.charAt(charPosition); message += replaceVal; }
System.out.println("Your encrypted message is:"); System.out.println(encrypt);
continue; } case 2: { sc.nextLine(); System.out.print("Enter the message :"); code = sc.nextLine(); String lowerline = code.toLowerCase();
// Decrypting the message cipherText = cipherText.toLowerCase(); message = ""; for (int ii = 0; ii < cipherText.length(); ii++) { int charPosition = code.indexOf(cipherText.charAt(ii)); char replaceVal = ALPHABET.charAt(charPosition); message += replaceVal; } break; } }
System.out.println("Your decrypted message is: "); System.out.println(message);
continue; } case 3: { break; }
default: { continue; }
}
break; }
I want you to fix this code please
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
