Advertisement
TheRightGuy

Feedback

Nov 16th, 2021
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. public class MorseCodeApp {
  2.     public static void main(String[] args) {
  3.         System.out.println("Enter your text to convert to morse code and play it ");
  4.         new letterMatcher();
  5.     }
  6. }
  7. -----------------------------------------------------------------------------------------
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.Scanner;
  11.  
  12. public class letterMatcher extends Initialize {
  13.  
  14.     letterMatcher(){
  15.         loopTroughInput();
  16.     }
  17.     private void loopTroughInput(){
  18.         int i = 0;
  19.         Scanner input = new Scanner(System.in);
  20.         String sampleMorseCodeInput = input.nextLine().toUpperCase();
  21.         while (i < sampleMorseCodeInput.length()) {
  22.             eachLetterToMorseCode(sampleMorseCodeInput, i);
  23.             pathForAudio(sampleMorseCodeInput, i);
  24.             i++;
  25.         }
  26.     }
  27.     private void eachLetterToMorseCode(String morse, int index) {
  28.         Map<Character, String> morseCode = new HashMap<>();
  29.         morseCode.put('A', ".- ");
  30.         morseCode.put('B', "-... ");
  31.         morseCode.put('C', "-.-. ");
  32.         morseCode.put('D', "-.. ");
  33.         morseCode.put('E', ". ");
  34.         morseCode.put('F', "..-. ");
  35.         morseCode.put('G', "--. ");
  36.         morseCode.put('H', ".... ");
  37.         morseCode.put('I', ".. ");
  38.         morseCode.put('J', ".--- ");
  39.         morseCode.put('K', "-.- ");
  40.         morseCode.put('L', ".-.. ");
  41.         morseCode.put('M', "-- ");
  42.         morseCode.put('N', "-. ");
  43.         morseCode.put('O', "--- ");
  44.         morseCode.put('P', ".--. ");
  45.         morseCode.put('Q', "--.- ");
  46.         morseCode.put('R', ".-. ");
  47.         morseCode.put('S', "... ");
  48.         morseCode.put('T', "- ");
  49.         morseCode.put('U', "..- ");
  50.         morseCode.put('V', "...- ");
  51.         morseCode.put('W', ".-- ");
  52.         morseCode.put('X', "-..- ");
  53.         morseCode.put('Y', "-.-- ");
  54.         morseCode.put('Z', "--.. ");
  55.         morseCode.put(' ', " / ");
  56.         // goes through the key and values, filter the map for keys that match the index of the char, gets and prints the values
  57.         morseCode.entrySet().stream().filter(map ->         map.getKey().equals(morse.charAt(index))).map(Map.Entry::getValue).forEach(System.out::print);
  58.     }
  59. }
  60. -----------------------------------------------------------------------------------------
  61.  
  62.  
  63. import javax.sound.sampled.AudioInputStream;
  64. import javax.sound.sampled.AudioSystem;
  65. import javax.sound.sampled.Clip;
  66. import javax.sound.sampled.LineUnavailableException;
  67. import javax.sound.sampled.UnsupportedAudioFileException;
  68.  
  69. import java.io.File;
  70. import java.io.IOException;
  71.  
  72. public class Initialize {
  73.     public void pathForAudio(String text, int index) {
  74.         String filePath = "src/main/resources/" + text.charAt(index) + ".wav";
  75.         audioSetUp(filePath);
  76.         sleepConditions(text, index);
  77.     }
  78.  
  79.     private void audioSetUp(String filePath)  {
  80.         try {
  81.             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
  82.             Clip clip = AudioSystem.getClip();
  83.             clip.open(audioInputStream);
  84.             clip.start();
  85.         }catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
  86.             e.printStackTrace();
  87.         }
  88.     }
  89.  
  90.  
  91.     private void sleepConditions(String morse, int index) {
  92.         try {
  93.             if (Character.isWhitespace(morse.charAt(index))) {
  94.                 Thread.sleep(700);
  95.             } else {
  96.                 indexConditions(index);
  97.             }
  98.         } catch (InterruptedException e) {
  99.             e.printStackTrace();
  100.         }
  101.     }
  102.     private void indexConditions(int index) throws InterruptedException {
  103.         if (index < 3) Thread.sleep(1300);
  104.         else Thread.sleep(1000);
  105.     }
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement