I am having trouble with this assignment. The prompt is to have the user enter a message to be encoded. The encoding will take the messages and shift each character 4 spaces to the right according to the ASCII table. I think I have the encrypt and decrypt methods correct (?) but I can't tell because I cannot figure out how to get the string that's entered into the methods.
import java.util.Scanner;
public class EncryptDecrypt {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to my encrypting/decrypting program");
System.out.println("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ");
System.out.println("(1) Encrypt a message.");
System.out.println("(2) Decrypt an encrypted message.");
System.out.println("(3) Exit.");
System.out.print("Your choice? ");
int choice = input.nextInt();
if (choice != 3) {
if (choice == 1) {
System.out.println("Please enter a message to be encrypted: ");
String message = input.nextLine();
System.out
.println("The encrypted string is " + encrypt(message));
}
else {
System.out.println("Please enter a string to be decrypted: ");
String encrypted = input.nextLine();
System.out.println(
"The decrypted string is " + decrypt(encrypted));
}
} else {
System.out.println("The program has been exited.");
}
}
public static String encrypt(String message) {
String encrypted = " ";
for (int i = 0; i < message.length(); i++) {
encrypted += (char) (message.charAt(i) + 4);
}
return encrypted;
}
public static String decrypt(String encrypted) {
String unencrypted = " ";
for (int i = 0; i < encrypted.length(); i++) {
unencrypted += (char) (encrypted.charAt(i) - 4);
}
return unencrypted;
}
}