Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SecretMessages {
- public static void main(String[] args){
- String message;
- do{
- //ask the user for a message to encode or decode
- System.out.println("Enter a message to encode or decode ");
- Scanner scan = new Scanner(System.in);
- message = scan.nextLine();
- String out = "";
- System.out.print("Enter a secret Key (-26 to 26)");
- try{
- int intKey = Integer.parseInt(scan.nextLine());
- char key = (char)intKey;
- for (int x = 0; x <= message.length()-1; x++){
- char in = message.charAt(x);
- if (in >= 'A' && in <= 'Z'){
- in += key;
- if (in > 'Z'){
- in -= 26;
- }
- if (in < 'A'){
- in += 26;
- }
- }
- if (in >= 'a' && in <= 'z'){
- in += key;
- if (in > 'z'){
- in -= 26;
- }
- if (in < 'a'){
- in += 26;
- }
- }
- out += in;
- }
- } catch(Exception e){
- System.out.println("INVALID INPUT YOU CRETIN! Read the instructions!");
- }
- System.out.println();
- System.out.println(out);
- System.out.println();
- System.out.println("Do you want to encode/decode another message?(Y/N)");
- message =(String) scan.nextLine();
- message.toUpperCase();
- System.out.println();
- } while (!message.equals("N"));
- }
- }
Add Comment
Please, Sign In to add comment