Advertisement
IronTomman

Untitled

Jan 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. package programming.set9.morses;
  2.  
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5.  
  6. public class MorseCoder {
  7.  
  8.     static String[] letters = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
  9.             "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
  10.     static String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
  11.             "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
  12.             "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----" };
  13.     static HashMap<String, String> letterToMorse = new HashMap<String, String>();
  14.     static HashMap<String, String> morseToLetter = new HashMap<String, String>();
  15.  
  16.     public void run() {
  17.         System.out.println(letters);
  18.     }
  19.  
  20.     /**
  21.      * Returns a new string which is the Morse code version of the input string.
  22.      * Each word in the output will be on a separate line. The Morse representation
  23.      * of each character of the input string is separated from the next by a space
  24.      * character in the output string.
  25.      *
  26.      * @param input the input string.
  27.      * @return the Morse code version of the input string, ignoring all characters
  28.      *         in the input string that cannot be represented in international Morse
  29.      *         code.
  30.      */
  31.     public static String encode(String input) {
  32.         String temp = "";
  33.         for (int i = 0; i < letters.length; i++) {
  34.             letterToMorse.put(letters[i], morse[i]);
  35.         }
  36.         String trimmed = input.trim().replaceAll(" +", " ");
  37.         trimmed = trimmed.toUpperCase();
  38.  
  39.         String encoded = "";
  40.  
  41.         String[] words = trimmed.split("\\s");
  42.         for (String word : words) {
  43.             encoded += "\n";
  44.             for (int i = 0; i < word.length(); i++) {
  45.                 boolean result = Arrays.stream(letters).anyMatch(String.valueOf(word.charAt(i))::equals);
  46.                 if (result) {
  47.                     encoded += letterToMorse.get(String.valueOf(word.charAt(i))) + " ";
  48.                 }else {
  49.                    
  50.                 }
  51.             }
  52.         }
  53.  
  54.         return encoded.substring(1);
  55.     }
  56.  
  57.     /**
  58.      * Returns a new string which is the natural-language version of the input
  59.      * string, which is assumed to be in Morse code format as produced by the encode
  60.      * method.
  61.      *
  62.      * @param input morse code input string.
  63.      * @return natural language version or {@code null} if the input string could
  64.      *         not be properly parsed.
  65.      */
  66.     public static String decode(String input) {
  67.         for (int i = 0; i < letters.length; i++) {
  68.             morseToLetter.put(morse[i], letters[i]);
  69.         }
  70.         String decoded = "";
  71.         String trimmed = input.trim().replaceAll(" +", " ");
  72.  
  73.         String[] words = trimmed.split("\\s{2}");
  74.         for (String word : words) {
  75.             decoded += " ";
  76.             String[] letters = word.split("\\s");
  77.             for (String letter : letters) {
  78.                 decoded += morseToLetter.get(letter);
  79.             }
  80.         }
  81.         return decoded.substring(1);
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement