?
2014-02-19 15:30:00 UTC
import java.util.Scanner;
import java.io.IOException;
public class MorseCodeTester
{
public static void main(String [] args)throws IOException
{
File file = new File("MorseCodeList.txt");
Scanner inFile = new Scanner(file);
String [] morseCode = new String[36];
String [] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " "};
int x = 0;
while(inFile.hasNextLine())
{
morseCode[x] = inFile.nextLine();
x++;
}
for(int i = 0; i < morseCode.length; i++)
{
System.out.println(morseCode[i]);
}
Scanner userInput = new Scanner(System.in);
int i = 0;
System.out.println("Please enter input with no punctuation");
String userString = userInput.nextLine();
System.out.println("");
MorseCode.convert(userString, morseCode, letters);
}
}
import java.util.Arrays;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class MorseCode
{
public MorseCode()
{
}
public static void convert (String toEncode, String [] letters, String [] morseCode)
{
String morse = toEncode;
int len = morse.length();
String [] convertedText = new String[len];
for(int y = 0; y < len; y++)
{
convertedText[y] = (morse.substring(y,y+1));
}
String [] morseCodeText = new String[len];
for(int i = 0; i < convertedText.length; i++)
{
for(int x = 0; x < 36; x++)
{
if(convertedText[i].equals (letters[x]))
{
morseCodeText[i] = morseCode[x];
}
else
{
x++;
}
}
i++;
}
for(int z = 0; z < morseCodeText.length; z++)
{
System.out.print(morseCodeText[z]);
}
}
}
The program is translating English to morse code. I am only printing the entire text file because I wanted to test it.
This is what the output looks like:
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
Please enter input with no punctuation
hello
nullnullnullnullnull
This is the text file:
.-
-…
-.-.
-..
.
..-.
—.
….
..
.—
-.-
.-..
--
-.
——
.—-.
—-.-
.-.
...
-
..-
...-
.—-
-..-
-.--
--..
.----
..---
...--
....-
.....
-....
--...
---..
----.
-----
(these are in order from a-z, followed by 0-9)