My code has to read morse code from a text file, ask a user for a sentence, then translate the user's input into morse code. This is what I've written:
import java.util.*; import java.io.*; public class MorseCode { public static void main(String[] args) throws Exception { Scanner inFile = new Scanner(new File("morsecode.txt")); Scanner in = new Scanner(System.in); System.out.print("Please enter a sentence to be translated to morse code: "); String userInput = in.nextLine(); String replacee[] = new String[36]; String replacer[] = new String[36]; String newInput[] = new String[36]; for(int i = 0 ; i < 36 ; i++) { replacee[i] = inFile.next(); replacer[i] = inFile.nextLine().replace(" ", "*"); newInput[i] = userInput.replace(replacer[i], replacee[i]); } System.out.println(newInput); } }
In case you're wondering, my text file looks something like this:
A .- B -... C -.-. D -.. E . F ..-. G --. H .... I .. [continuing with all of the letters] 0 ----- 1 .---- 2 ..--- 3 ...-- 4 ....- 5 ..... 6 -.... 7 --... 8 ---.. 9 ----.