Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MorseCodeApp {
- public static void main(String[] args) {
- System.out.println("Enter your text to convert to morse code and play it ");
- new letterMatcher();
- }
- }
- -----------------------------------------------------------------------------------------
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class letterMatcher extends Initialize {
- letterMatcher(){
- loopTroughInput();
- }
- private void loopTroughInput(){
- int i = 0;
- Scanner input = new Scanner(System.in);
- String sampleMorseCodeInput = input.nextLine().toUpperCase();
- while (i < sampleMorseCodeInput.length()) {
- eachLetterToMorseCode(sampleMorseCodeInput, i);
- pathForAudio(sampleMorseCodeInput, i);
- i++;
- }
- }
- private void eachLetterToMorseCode(String morse, int index) {
- Map<Character, String> morseCode = new HashMap<>();
- morseCode.put('A', ".- ");
- morseCode.put('B', "-... ");
- morseCode.put('C', "-.-. ");
- morseCode.put('D', "-.. ");
- morseCode.put('E', ". ");
- morseCode.put('F', "..-. ");
- morseCode.put('G', "--. ");
- morseCode.put('H', ".... ");
- morseCode.put('I', ".. ");
- morseCode.put('J', ".--- ");
- morseCode.put('K', "-.- ");
- morseCode.put('L', ".-.. ");
- morseCode.put('M', "-- ");
- morseCode.put('N', "-. ");
- morseCode.put('O', "--- ");
- morseCode.put('P', ".--. ");
- morseCode.put('Q', "--.- ");
- morseCode.put('R', ".-. ");
- morseCode.put('S', "... ");
- morseCode.put('T', "- ");
- morseCode.put('U', "..- ");
- morseCode.put('V', "...- ");
- morseCode.put('W', ".-- ");
- morseCode.put('X', "-..- ");
- morseCode.put('Y', "-.-- ");
- morseCode.put('Z', "--.. ");
- morseCode.put(' ', " / ");
- // goes through the key and values, filter the map for keys that match the index of the char, gets and prints the values
- morseCode.entrySet().stream().filter(map -> map.getKey().equals(morse.charAt(index))).map(Map.Entry::getValue).forEach(System.out::print);
- }
- }
- -----------------------------------------------------------------------------------------
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.Clip;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.UnsupportedAudioFileException;
- import java.io.File;
- import java.io.IOException;
- public class Initialize {
- public void pathForAudio(String text, int index) {
- String filePath = "src/main/resources/" + text.charAt(index) + ".wav";
- audioSetUp(filePath);
- sleepConditions(text, index);
- }
- private void audioSetUp(String filePath) {
- try {
- AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
- Clip clip = AudioSystem.getClip();
- clip.open(audioInputStream);
- clip.start();
- }catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
- e.printStackTrace();
- }
- }
- private void sleepConditions(String morse, int index) {
- try {
- if (Character.isWhitespace(morse.charAt(index))) {
- Thread.sleep(700);
- } else {
- indexConditions(index);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- private void indexConditions(int index) throws InterruptedException {
- if (index < 3) Thread.sleep(1300);
- else Thread.sleep(1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement