Error checking
``` import java.lang.*; import java.util.*; import java.io.*; public class TopSecretEncoder { public static String alpha = "abcdefghijklmnopqrstuvwxyz"; public static void main (String [] args) { int option =0; do { System.out.println("Top Secret word Encrypt / Decrypt "); System.out.println("Enter an option: "); System.out.println("(1) to encrypt a word"); System.out.println("(2) to decrypt a word"); System.out.println("(0) to exit"); System.out.println("Option : "); option = In.getInt(); if (option == 1) { encryption(); } else if (option == 2) { decryption(); } else if (option == 0) { System.exit(0); } else { do { System.out.println("Not a valid option!"); option = In.getInt(); } while (option != 0 && option != 1 && option != 2); } } while (option != 0); } public static void encryption() { int keyNum = 0; String encrypted = ""; String alphaShift = ""; System.out.println("Please enter the word you would like to encrypt."); String word = In.getString(); System.out.println("Please enter an encryption key."); String key = In.getString(); for (int i = 0; i < key.length(); i++) { keyNum = keyNum + alpha.indexOf(key.charAt(i)) + 1; } while (keyNum > 26) { if (keyNum > 26) { keyNum = keyNum - 26; } } alphaShift = alpha.substring(alpha.length() - keyNum) + alpha.substring (0, alpha.length() - keyNum); System.out.println(keyNum); System.out.println(alphaShift); for (int i = 0; i < word.length(); i++) { encrypted = encrypted + (alphaShift.charAt(alpha.indexOf(word.charAt(i)))); } System.out.println("Your encrypted word is: " + encrypted); } public static void decryption() { String decrypted = ""; int keyNum = 0; System.out.println("Please enter the word you would like to decrypt."); String word = In.getString(); System.out.println("Please enter a decryption key."); String key = In.getString(); for (int i = 0; i < key.length(); i++) { keyNum = keyNum + alpha.indexOf(key.charAt(i)) + 1; } System.out.println(keyNum); while (keyNum > 26) { if (keyNum > 26) { keyNum = keyNum - 26; } } for (int i = 0; i < word.length(); i++) { if ((alpha.indexOf(word.charAt(i)) + keyNum) >= 26) { decrypted = decrypted + alpha.charAt((alpha.indexOf(word.charAt(i))) + keyNum - alpha.length() ); } else { decrypted = decrypted + alpha.charAt((alpha.indexOf(word.charAt(i))) + keyNum); } } System.out.println("Your decrypted word is: " + decrypted); } } ```
ok so, here is my code. When I run the code, if the user types in 5 as an option instead of 1, 2, or 0, I want the error message to be printed and then the user to be re-prompted. What it does not is it prints the error message and re-prompts, but if I enter 1 into the re-prompt box, it will, instead of going straight to option 1, encryption, it will first print the menu and all again which is what my loop currently does. The reason why I had a loop in a loop was so that after a user does their encryption/decryption they will be presented with the menu again, so any way to keep that but change it so that for re-prompting we don't need to see the menu again?
You have to put the input loop after the meny, but before the methods (encrypt, decrypt, exit) are called, i.e you have to move the nested/inner do-while loop to part between the menu and the the if-, else if statements. Like this ``` System.out.println("Top Secret word Encrypt / Decrypt "); System.out.println("Enter an option: "); ... do { System.out.println("Not a valid option!"); ... } while (...) if (option == 1) { ... } else if (option == 2) { ... } else if (option == 0) { ... } ``` I replaced some of your code with dots to keep the example short. Also note how the else part isn't necessary any more. You also have to create some if-statement in the do-while you moved to keep it from printing System.out.println("Not a valid option!"); when it shouldn't or maybe use a regular while loop instead of a do-while.
Thank you
You can also use try and catch blocks or exception handling
Join our real-time social learning platform and learn together with your friends!