Guest User

/r/learnjava

a guest
Feb 10th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class SecretMessages {
  3.    
  4.     public static void main(String[] args){
  5.        
  6.         String message;
  7.         do{
  8.             //ask the user for a message to encode or decode
  9.             System.out.println("Enter a message to encode or decode ");
  10.            
  11.             Scanner scan = new Scanner(System.in);
  12.             message = scan.nextLine();
  13.             String out = "";
  14.            
  15.             System.out.print("Enter a secret Key (-26 to 26)");
  16.            
  17.             try{
  18.                 int intKey = Integer.parseInt(scan.nextLine());
  19.                 char key = (char)intKey;
  20.                
  21.                 for (int x = 0; x <= message.length()-1; x++){
  22.                    
  23.                     char in = message.charAt(x);
  24.                    
  25.                     if (in >= 'A' && in <= 'Z'){
  26.                         in += key;
  27.                         if (in > 'Z'){
  28.                             in -= 26;
  29.                         }
  30.                         if (in < 'A'){
  31.                             in += 26;
  32.                         }
  33.                     }
  34.                    
  35.                     if (in >= 'a' && in <= 'z'){
  36.                         in += key;
  37.                         if (in > 'z'){
  38.                             in -= 26;
  39.                         }
  40.                         if (in < 'a'){
  41.                             in += 26;
  42.                         }
  43.                     }
  44.                    
  45.                         out += in;
  46.                 }
  47.             } catch(Exception e){
  48.                 System.out.println("INVALID INPUT YOU CRETIN! Read the instructions!");
  49.             }
  50.            
  51.             System.out.println();
  52.             System.out.println(out);
  53.             System.out.println();
  54.             System.out.println("Do you want to encode/decode another message?(Y/N)");
  55.            
  56.             message =(String) scan.nextLine();
  57.             message.toUpperCase();
  58.            
  59.             System.out.println();
  60.         } while (!message.equals("N"));
  61.     }
  62.  
  63. }
Add Comment
Please, Sign In to add comment