简体   繁体   中英

How can I have my translator detect the morse code as a string, not a char

Here is my code. I am having an issue. It works just fine but when you convert morse code to english, it only prints out the morse code single digit letters. Can someone give me a solution or at least help me understand what is wrong because it is immensely frustrating.

Here is the bit of my code that matters

An example of the problem is when i put in .- it printed e and t , not a .

public class Project1
{
public static void main( String [] args )
{
    System.out.println();
    choice();

}
    public static void choice()
{
    int user_choice = 0; 
    user_choice = Input.getInt("Enter 1 if you want to change English to Morse code, and enter 2 to change Morse code to English");
    if(user_choice == 1)
    {
    String output = new String();
    String inital = new String();
    inital = english_to_morse();

    for( int k = 0; k < inital.length(); k++)
    {
        output += morse(inital.charAt( k ));
    }

        System.out.print(output);

    }
    if(user_choice == 2)
    {
    String output2 = new String();
    String inital2 = new String();
    inital2 = morse_to_english();

    for( int k = 0; k < inital2.length(); k++)
    {
        System.out.println("#####"+String.valueOf(inital2.charAt( k ))+"#####");
        output2 += english(String.valueOf(inital2.charAt( k )));
    }
        System.out.print(output2);
    }
}

public static String english_to_morse() 
{
  String user_input = new String();

  user_input = Input.getString("Enter a phrase and I'll convert it to Morse Code");

  return user_input.toLowerCase();
}

public static String morse_to_english() 
{
  String user_input = new String();

  user_input = Input.getString("Enter a phrase in Morse Code and I'll convert it to English");

  return user_input.toLowerCase();
}

public static String morse(char letter)
{
    String output = new String();
    char[] alphabet_numbers = {'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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ' };
    String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };

    for( int j = 0; j < alphabet_numbers.length; j++ )
    {
        if (alphabet_numbers[j]==letter)
        {
            output = morse_code[j];
        }
    }
    return output + " ";
}   
public static String english(String letter)
{
    String output = new String();
    String alphabet_numbers[] = {"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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " " };
    String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };

    for( int j = 0; j < morse_code.length; j++ )
    {
        if (morse_code[j].equals(letter))
        {
            output = alphabet_numbers[j];
        }
    }
    return output + " ";
}   
}

Well the reason why .- is giving et instead of a is due to the way you're reading the string.

Your code reads . and then looks up the table to determine whether it corresponds to any alpha character, in which case it does: e . Then you read a - and you look it up and you get a t .

If your input is literally just

.-.---.-.-.........-----.-.-.-.-

You're pretty much stuck because you don't know when one ends and another begins. As another example, how should one distinguish between the following strings

.
..
...
....

They are all equally valid signals, but depending on how you interpret it, you get very different results.

You can't say something like "I'll just take the longest matching string" because there is no reason why that is a valid rule.

If the sample input I provided above does not match your input, you should indicate what your input is.

The problem is in how you iterate your strings.

When going from English to Morse, it is ok to just iterate single characters as you do here:

for( int k = 0; k < inital.length(); k++) {
    output += morse(inital.charAt( k ));
}

but when going from Morse to English you have to iterate several characters at once, because a symbol in Morse generally spans several characters. For instance, the Morse string .- -... -.-. has three symbols that correspond to abc in English, but they have 2, 4 and 4 characters each.

So when iterating your Morse string, you have to split it by spaces, and iterate each of the substrings. In the case above, you'll iterate .- , then -... and then -.-. :

for(String symbol : inital2.split(" ")){
    output2 += english(symbol);
}

Your problem is this line

output2 += english(String.valueOf(inital2.charAt( k )));

You give ".-" as input, but it does not convert ".-", because it takes each character of the string ".-" and converts it, which gives your "et" result.

To fix this you have to use a separator in your morse code input. You already somehow defined " " as a separator so I will use it.

if(user_choice == 2){ String output2 = new String(); String[] inital2 = morse_to_english().split(" "); for( int k = 0; k < inital2.length; k++){ output2 += english(inital2[k]); } System.out.print(output2); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM