Thomas
2012-07-22 12:44:01 UTC
import java.io.*;
class TranslatorMorse {
public static void main( String [] args ) {
// Create strings for the English alphabet and numbers
String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "; // ("space" character included for convenience)
String numbers = "1234567890";
// Create arrays for Morse Code alphabet and numbers
String lettersMorse[] = { ".--", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "|" };
String numbersMorse[] = { ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----" };
// Ask the user to specify the desired type of translation
String type = JOptionPane.showInputDialog( "Type \"1\" if you would like to translate from English to Morse Code. Type \"2\" if you would like to translate from Morse Code to English." );
// English to Morse
if ( type.equals( "1" ) ) {
char character;
// Receive input string
String inputStringOriginal = JOptionPane.showInputDialog( "Enter the English you would like to translate to Morse Code using only letters, digits, and spaces:" );
String inputString = inputStringOriginal.toUpperCase();
// Create an output string
String output[] = new String[inputString.length()];
int x;
int y;
int a;
int b;
// Translate each individual character
for ( x = 0; x < inputString.length(); x++ ) {
character = inputString.charAt( x );
// Translate letters and spaces
for ( y = 0; y < letters.length(); y++ ) {
if (character == letters.charAt( y ))
output[x] = lettersMorse[y];
}
// Translate digits
for ( b = 0; b < numbers.length(); b++ ) {
if (character == numbers.charAt( b ))
output[x] = numbersMorse[b];
}
}
// Display the translated results
int z = 0;
for ( z = 0; z < output.length; z++ ) {
System.out.print( output[z] + " " );
}
}
// Morse to English
if ( type.equals( "2" ) ) {
char character;
// Receive input string
String inputStringOriginal = JOptionPane.showInputDialog( "Enter the Morse Code you would like to translate to English using the \"-\" (hyphen) as a dash and the \".\" (period) as a dot. Separate letters with spaces and indicate spaces between words with one \"|\" with a space on either side of it." );
// Add a space to the original input for later substring isolation
String inputString = inputStringOriginal + " ";
// Translate and display
int c = 0;
int d;
int e;
int f;
// Go through all the characters in the inputted sequence
for ( d = 0; d < inputString.length(); d++ ) {
character = inputString.charAt( d );
String characterString = "" + character;
// Stop when " " (space) is found and isolate previous Morse Code letter as string
if ( characterString.equals( " " ) ) {
String morseLetter = inputString.substring ( c, d );
// Translate isolated string if letter or space and print
for ( e = 0; e < letters.length(); e++ ) {
if ( morseLetter.equals( lettersMorse[e] ) )
System.out.print( letters.charAt( e ) );
}
// Translate isolated string if number and print
for ( f = 0; f < numbers.length(); f++ ) {
if ( morseLetter.equals( numbersMorse[f] ) )
System.out.print( numbers.charAt( f ) );
}
c = d;
}
}
}
}
}