Hi! I need help for my program. This program encrypts and decrypts a string. It is actually working but I want to make changes.
In my program, when you choose to encrypt a word, it encrypts the word then it ends its process. I want to go back to the "main option" where the user can decrypt the string he just encrypted. How do you do that?
Here's the code:
import java.io.*; class MC14{ static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in)); static PrintWriter screen=new PrintWriter(System.out, true); //Setting up input/output variables. public static void main(String[] args) throws IOException { int choice; String str; screen.println("ENCRYPT & DECRYPT"); screen.println(); screen.println("Please enter the text to be encrypted/decrypted."); screen.println(); str = keyboard.readLine(); screen.println(); screen.println(); screen.println("Choose an option: "); screen.println(); screen.println("1) Encrypt string. \n2) Decrypt string."); screen.println(); choice = new Integer(keyboard.readLine()).intValue(); switch(choice){ case 1: screen.println("Encrypted string:"); screen.println(); screen.println(encrypt(str)); screen.println(); break; case 2: screen.println("Decrypted string:"); screen.println(); screen.println(decrypt(str)); screen.println(); break; } } public static String encrypt(String str) { str = str.toLowerCase(); //convert string to lower case to avoid problems int length = str.length(); int i,count,inc; char[] array = new char[length]; //variables used in the method String encrypted; for(i=0;i<length;i++){array[i] = str.charAt(i);} //Places each character in the string //into an array index. for(count=0;count<array.length;count++){ //for each item in the array the following switch is performed switch(array[count]){ //This switch statement changes the character //within each array index. This is the start case 'a': //of the encryption. array[count] = '7'; break; case 'b': array[count] = 'k'; break; case 'c': array[count] = 'r'; break; case 'd': array[count] = 's'; break; case 'e': array[count] = '3'; break; case 'f': array[count] = 'e'; break; case 'g': array[count] = 'a'; break; case 'h': array[count] = 'o'; break; case 'i': array[count] = '8'; break; case 'j': array[count] = ','; break; case 'k': array[count] = 'b'; break; case 'l': array[count] = 'x'; break; case 'm': array[count] = 'c'; break; case 'n': array[count] = 'h'; break; case 'o': array[count] = '5'; break; case 'p': array[count] = '?'; break; case 'q': array[count] = 'd'; break; case 'r': array[count] = '4'; break; case 's': array[count] = 'f'; break; case 't': array[count] = 'm'; break; case 'u': array[count] = 'z'; break; case 'v': array[count] = 'g'; break; case 'w': array[count] = '1'; break; case 'x': array[count] = 'i'; break; case 'y': array[count] = '9'; break; case 'z': array[count] = 'j'; break; case '1': array[count] = '6'; break; case '2': array[count] = 'p'; break; case '3': array[count] = '.'; break; case '4': array[count] = '!'; break; case '5': array[count] = '0'; break; case '6': array[count] = 'l'; break; case '7': array[count] = 'y'; break; case '8': array[count] = 'n'; break; case '9': array[count] = 'u'; break; case '0': array[count] = 'q'; break; case '.': array[count] = 'w'; break; case '!': array[count] = 'v'; break; case '?': array[count] = '2'; break; case ',': array[count] = 't'; break; case ' ': array[count] = ' '; break; } } encrypted = String.valueOf(array); //constructing a String object //from char array[] StringBuffer reversed = new StringBuffer(encrypted); //creating a StringBuffer object from String encrypted. reversed.reverse(); //reversing order within StringBuffer object. encrypted = new String(reversed); //setting the encrypted String from //StringBuffer object reversed. return encrypted; //returning String encrypted. } // END OF METHOD encrypt(); public static String decrypt(String str){ str = str.toLowerCase(); //convert string to lower case to avoid problems int length = str.length(); int i,count; //variable used in the method char[] array = new char[length]; StringBuffer reversed = new StringBuffer(str); reversed.reverse(); //reverses the encrypted string String encrypted = new String(reversed); for(i=0;i<length;i++){array[i] = encrypted.charAt(i);} //places all characters of string into array for(count=0;count<length;count++){ switch(array[count]){ //This switch statement changes the character //within each array index. This is the start case 'a': //of the decryption. array[count] = 'g'; break; case 'b': array[count] = 'k'; break; case 'c': array[count] = 'm'; break; case 'd': array[count] = 'q'; break; case 'e': array[count] = 'f'; break; case 'f': array[count] = 's'; break; case 'g': array[count] = 'v'; break; case 'h': array[count] = 'n'; break; case 'i': array[count] = 'x'; break; case 'j': array[count] = 'z'; break; case 'k': array[count] = 'b'; break; case 'l': array[count] = '6'; break; case 'm': array[count] = 't'; break; case 'n': array[count] = '8'; break; case 'o': array[count] = 'h'; break; case 'p': array[count] = '2'; break; case 'q': array[count] = '0'; break; case 'r': array[count] = 'c'; break; case 's': array[count] = 'd'; break; case 't': array[count] = ','; break; case 'u': array[count] = '9'; break; case 'v': array[count] = '!'; break; case 'w': array[count] = '.'; break; case 'x': array[count] = 'l'; break; case 'y': array[count] = '7'; break; case 'z': array[count] = 'u'; break; case '1': array[count] = 'w'; break; case '2': array[count] = '?'; break; case '3': array[count] = 'e'; break; case '4': array[count] = 'r'; break; case '5': array[count] = 'o'; break; case '6': array[count] = '1'; break; case '7': array[count] = 'a'; break; case '8': array[count] = 'i'; break; case '9': array[count] = 'y'; break; case '0': array[count] = '5'; break; case '.': array[count] = '3'; break; case '!': array[count] = '4'; break; case '?': array[count] = 'p'; break; case ',': array[count] = 'j'; break; case ' ': array[count] = ' '; break; } } String decrypted = new String (String.valueOf(array)); //constructing string from array of char. return decrypted; } //END OF METHOD decrypt() } //END OF CLASS Encrypto