Question:
JAVA: Why is my code not working? Only the Morse -> English isn't working properly, and I don't know why.?
Thomas
2012-07-22 12:44:01 UTC
import javax.swing.JOptionPane;
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;
}
}
}
}
}
Three answers:
indi01
2012-07-22 17:03:27 UTC
I think there's a typo in the letters...both A and X are mapped to ".--".



Also don't try to reinvent the wheel...there's a wonderful method in the String class called split() that takes a regular expression and returns an array of all the words separated by that regex (so in this case a single space is the pattern to match)...



here's how I would correct it:



import javax.swing.JOptionPane;

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 e;

int f;



//tokenising the string splitting on spaces

String[] tokens = inputString.split(" ");



// Translate isolated string if letter or space and print

for (int i = 0; i < tokens.length; i++)

{

for ( e = 0; e < letters.length(); e++ ) {

if ( tokens[i].equals( lettersMorse[e] ) )

System.out.print( letters.charAt( e ) );

}

// Translate isolated string if number and print

for ( f = 0; f < numbers.length(); f++ ) {

if ( tokens[i].equals( numbersMorse[f] ) )

System.out.print( numbers.charAt( f ) );

}



}

}

}



}
bachinski
2016-10-07 09:35:40 UTC
Morse To English
2016-02-22 02:52:07 UTC
Amzanig


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...