Advertisement
andkamen

encrypt

Mar 4th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. package com.Olympiad2016;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class t {
  7.     public static void main(String[] args) {
  8.         Scanner console = new Scanner(System.in);
  9.  
  10.         while (true) {
  11.             String inputStart = console.nextLine();
  12.  
  13.             if (inputStart.equals("START") || inputStart.equals("start")) {
  14.                 break;
  15.             }
  16.         }
  17.  
  18.         int messageCount = 0;
  19.  
  20.         ArrayList<String> output = new ArrayList<>();
  21.         StringBuilder encryptedMessage = new StringBuilder();
  22.  
  23.         while (true) {
  24.  
  25.             String input = console.nextLine();
  26.             if (input.equals("END") || input.equals("end")) {
  27.                 break;
  28.             }
  29.  
  30.             if (input.equals("")) {
  31.                 continue;
  32.             }
  33.             messageCount++;
  34.             for (char ch : input.toCharArray()) {
  35.                 // For the digits 0 -> 1
  36.                 if (ch >= 48 && ch <= 57) {
  37.                     encryptedMessage.append(ch);
  38.                     // For upperCase letters A -> M
  39.                 } else if (ch >= 65 && ch <= 77) {
  40.                     encryptedMessage.append((char) (ch + 13));
  41.                     // For lowerCase letters a -> m
  42.                 } else if (ch >= 97 && ch <= 109) {
  43.                     encryptedMessage.append((char) (ch + 13));
  44.                     // For upperCase letters N -> Z
  45.                 } else if (ch >= 78 && ch <= 90) {
  46.                     encryptedMessage.append((char) (ch - 13));
  47.                     // For lowerCase letters n -> a
  48.                 } else if (ch >= 110 && ch <= 122) {
  49.                     encryptedMessage.append((char) (ch - 13));
  50.                     // For the special characters
  51.                 } else if (ch == 32) {
  52.                     encryptedMessage.append((char) 43);
  53.                 } else if (ch == 44) {
  54.                     encryptedMessage.append((char) 37);
  55.                 } else if (ch == 46) {
  56.                     encryptedMessage.append((char) 38);
  57.                 } else if (ch == 63) {
  58.                     encryptedMessage.append((char) 35);
  59.                 } else if (ch == 33) {
  60.                     encryptedMessage.append((char) 36);
  61.                 } else if (ch == 43) {
  62.                     encryptedMessage.append((char) 32);
  63.                 } else if (ch == 37) {
  64.                     encryptedMessage.append((char) 44);
  65.                 } else if (ch == 38) {
  66.                     encryptedMessage.append((char) 46);
  67.                 } else if (ch == 35) {
  68.                     encryptedMessage.append((char) 63);
  69.                 } else if (ch == 36) {
  70.                     encryptedMessage.append((char) 33);
  71.                 }
  72.             }
  73.             output.add(encryptedMessage.reverse().toString());
  74.             //clear the stringBuilder for the next message
  75.             encryptedMessage.setLength(0);
  76.         }
  77.         if (messageCount == 0) {
  78.             System.out.println("No messages sent.");
  79.         } else {
  80.             System.out.println("Total number of messages: " + messageCount);
  81.         }
  82.  
  83.         for (String string : output) {
  84.             System.out.println(string);
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement