Question: hello I'm trying to Code a program in java to do two way conversions of morse code, so from code to English and from English
hello I'm trying to Code a program in java to do two way conversions of morse code, so from code to English and from English to code I'm struggling with getting input from the user can you please help me I'm trying to add scanner but I'm not sure how
here is the code
package morseCodeHomework;
import java.util.*;
import java.util.Scanner;
import java.util.InputMismatchException;
import jdk.internal.util.xml.impl.Input;
public class MorseCode {
public static void main(String[] args) {
getInput();
}
public static void getInput() {
char a;
a = Input.getChar("Enter m for Morse to English or e for English to Morse.");
if (a == 'm')
translateMorse();
else if (a == 'e')
translateEnglish();
else
System.out.println("Your input is not valid.");
}
public static void translateEnglish() {
String englishToMorse = Input.getString("Enter a phrase in English.");
englishToMorse = englishToMorse.toLowerCase();
int i = 0;
while(i Map morse.put('a', ".-"); morse.put('b', "-..."); morse.put('c', "-.-"); morse.put('d', "-.."); morse.put('e', "."); morse.put('f', "..-."); morse.put('g', "--."); morse.put('h', "...."); morse.put('i', ".."); morse.put('j', ".---"); morse.put('k', "-."); morse.put('l', ".-.."); morse.put('m', "--"); morse.put('n', "-."); morse.put('o', "---"); morse.put('p', ".--."); morse.put('q', "--.-"); morse.put('r', ".-."); morse.put('s', "..."); morse.put('t', "-"); morse.put('u', "..-"); morse.put('v', "...-"); morse.put('w', ".--"); morse.put('x', "-..-"); morse.put('y', "-.--"); morse.put('z', "--.."); morse.put('1', ".----"); morse.put('2',"..---"); morse.put('3', "...--"); morse.put('4', "....-"); morse.put('5', "....."); morse.put('6', "-...."); morse.put('7', "--..."); morse.put('8', "---.."); morse.put('9', "----."); morse.put('0', "-----"); System.out.print(morse.get(englishToMorse.charAt(i))); i++; } } public static void translateMorse() { String morseToEnglish = Input.getString("Enter a phrase in Morse Code."); morseToEnglish = morseToEnglish.toLowerCase(); int i = 0; while(i < morseToEnglish.length() ) { HashMap morse.put('a', ".-"); morse.put('b', "-..."); morse.put('c', "-.-"); morse.put('d', "-.."); morse.put('e', "."); morse.put('f', "..-."); morse.put('g', "--."); morse.put('h', "...."); morse.put('i', ".."); morse.put('j', ".---"); morse.put('k', "-."); morse.put('l', ".-.."); morse.put('m', "--"); morse.put('n', "-."); morse.put('o', "---"); morse.put('p', ".--."); morse.put('q', "--.-"); morse.put('r', ".-."); morse.put('s', "..."); morse.put('t', "-"); morse.put('u', "..-"); morse.put('v', "...-"); morse.put('w', ".--"); morse.put('x', "-..-"); morse.put('y', "-.--"); morse.put('z', "--.."); morse.put('1', ".----"); morse.put('2',"..---"); morse.put('3', "...--"); morse.put('4', "....-"); morse.put('5', "....."); morse.put('6', "-...."); morse.put('7', "--..."); morse.put('8', "---.."); morse.put('9', "----."); morse.put('0', "-----"); Collection for (String string: values) { System.out.print(string); } i++; } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
