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 ");
- InputHandler inputHandler = new InputHandler();
- inputHandler.loopTroughInput();
- }
- }
- -------------
- public class InputHandler {
- private final Map<String, String> letterToMorseCode = Map.ofEntries(
- Map.entry("A", ".-"),
- Map.entry("B", "-..."),
- Map.entry("C", "-.-."),
- Map.entry("D", "-.."),
- Map.entry("E", "."),
- Map.entry("F", "..-."),
- Map.entry("G", "--."),
- Map.entry("H", "...."),
- Map.entry("I", ".."),
- Map.entry("J", ".---"),
- Map.entry("K", "-.-"),
- Map.entry("L", ".-.."),
- Map.entry("M", "--"),
- Map.entry("N", "-."),
- Map.entry("O", "---"),
- Map.entry("P", ".--."),
- Map.entry("Q", "--.-"),
- Map.entry("R", ".-."),
- Map.entry("S", "..."),
- Map.entry("T", "-"),
- Map.entry("U", "..-"),
- Map.entry("V", "...-"),
- Map.entry("W", ".--"),
- Map.entry("X", "-..-"),
- Map.entry("Y", "-.--"),
- Map.entry("Z", "--.."));
- void loopTroughInput() {
- AudioHandler audioHandler = new AudioHandler();
- Scanner input = new Scanner(System.in);
- String userInput = input.nextLine().toUpperCase();
- Matcher matcher = Pattern.compile("([a-zA-Z])").matcher(userInput);
- while (matcher.find()) {
- getMatchingValue(matcher.group(1));
- audioHandler.pathForAudio(matcher.group(1));
- }
- }
- private void getMatchingValue(String key) {
- letterToMorseCode.entrySet().stream().filter(map -> map.getKey().equals(key)).map(Map.Entry::getValue).forEach(morse -> System.out.print(morse + " "));
- }
- }
- --------------------
- public class AudioHandler {
- void pathForAudio(String letter) {
- String filePath = "src/main/resources/" + letter + ".wav";
- audioSetUp(filePath);
- breakBetweenLetters();
- }
- private void breakBetweenLetters() {
- try {
- Thread.sleep(1500);
- } catch (InterruptedException e) {
- System.out.println("Something is wrong with the thread");
- }
- }
- private void audioSetUp(String filePath) {
- try {
- AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Paths.get(filePath).toFile());
- Clip clip = AudioSystem.getClip();
- clip.open(audioInputStream);
- clip.start();
- } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
- System.out.println("That file is either invalid or there was a mistake during the playing of audio");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment