简体   繁体   中英

Why is my Java Morse Code translator giving me the wrong output?

I have seen many questions about the Morse Code translator floating around and have looked at many of them, but in all of them the suggested answer gives me the same incorrect output. The idea behind the code is to be able to translate Morse Code to English and vice versa using arrays. My code is as follows:

import java.util.Scanner;
public static void main ( String [] args )
{

    String [] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|"};

    String [] english =  { "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", " "};

    Scanner input = new Scanner ( System. in );
    System.out.println ( "Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
    int userChoice = input.nextInt();
    String translateMe;

    while (userChoice < 1 || userChoice > 2 ) // Ensures user enters a valid choice
    {
        System.out.println( "Not a valid entry. Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
        userChoice = input.nextInt();
    }

   if (userChoice == 1 )
    {
        System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '" );
        translateMe = input.next();

        String [] morseChar = translateMe.split(" ");

        for( int x = 0; x < morseChar.length; x++)
        {
            String letter = morseChar[ x ];
            for ( int index = 0; index < morse.length; index++ )
            {
                if(morse [ index ].equals(letter))
                {
                    System.out.print(english[ index ]); // Display character at matching index position of English array to show translation
                }
            }
        }

        }

    else
    {
        System.out.println("Please enter an English statement to translate:");
        translateMe = input.next();
        translateMe = translateMe.toLowerCase();

        String [] englishChar = translateMe.split("(?!^)");

        for ( int x = 0; x < englishChar.length; x++)
        {
            String letter = englishChar [ x ];

            for (int index = 0; index < english.length; index++)
            {
                if( english [index].equals( letter ))
                {
                    System.out.print(morse[index] + " "); // Display Morse Code array character at matching index position to show translation
                }

            }
        }

    }

}

}

I have been using the phrase

to be

and its Morse Code counterpart

- --- | -… .

as a test phrase. When I try translating English to Morse Code with this phrase I get

... -. s and n

as the output. When I try Morse Code to English, I get

u

as the output. I have gone over my two String arrays to make sure morse[A] and english[A] are in the same index position and so on, and those are fine. I can't think of anything else that would cause this problem.

Edit: It may be helpful to know I'm using IntelliJ IDEA 15

  1. The code for n ( "-." ) appears twice in your array.
  2. You are probably better off converting the String into a char[] . You'd have to change the alphabet from String[] to char[] .
  3. This is why we have Map .

The translation from morse to English is not working due to your use of Scanner. You need to use nextLine() as follows:

if (userChoice == 1 )
{
    translateMe = input.nextLine();
    System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '" );
    translateMe = input.nextLine();

After that, it seems to translate fine, even with the split command.

Output:

[.-, -..., -.-.]
abc

Output 2:

Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '
- --- | -... .
[-, ---, |, -..., .]
to be

I can't understand the output you get--while I haven't tried running it, by eyeballing it I see a very different result.

Fundamentally, though, your morse-to-English translator is matching too early. You need to check the whole string.

Edit: Now nothing obvious sticks out, have you tried stepping through it with the debugger to see what's actually going on?

The reason you are getting "s" and "n" from the english translation is because if we inspect the word "to", the previous letter in the alphabet to "t" is "s" and the previous in the alphabet to "o" is "n", so that leads me to believe there is a looping error in there. Additionally, you're using "next()" instead of "nextLine()" so you're not parsing what you think you are. I think if you use "nextLine()" and fix your loop, you'll have it. Thanks for providing full source code! :D

Here's how I tested:

import java.util.Scanner;

public class Main {

public static void main ( String [] args )
{

    String [] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|"};

    String [] english =  { "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", " "};

    Scanner input = new Scanner ( System. in );
    System.out.println ( "Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
    int userChoice = input.nextInt();
    String translateMe;

    while (userChoice < 1 || userChoice > 2 ) // Ensures user enters a valid choice
    {
        System.out.println( "Not a valid entry. Enter 1 to translate Morse Code to English or 2 to translate English to Morse Code:");
        userChoice = input.nextInt();
    }

    if (userChoice == 1 )
    {
        System.out.println("Please enter a Morse Code statement to translate. Separate letters with spaces and words with a ' | '" );
        translateMe = input.next();

        String [] morseChar = translateMe.split(" ");

        for( int x = 0; x < morseChar.length; x++)
        {
            System.out.println("Comparing " + morseChar + " to the morse array");
            for ( int index = 0; index < morse.length; index++ )
            {
                System.out.println("Comparing equality of: " + morseChar[x] + " and " + morse[index]);
                if(morseChar[x].equals(morse[index]))
                {
                    System.out.print(english[ index ]); // Display character at matching index position of English array to show translation
                }
            }
        }

    }

    else
    {
        System.out.println("Please enter an English statement to translate:");
        translateMe = input.next();
        translateMe = translateMe.toLowerCase();
        System.out.println(" Translating:  [" + translateMe + "]");

        System.out.println("Translating: " + translateMe);
        String[] englishChar = translateMe.split(" ");

        for(String s : englishChar) {
            System.out.println("Translation String: " + s);
        }

        for ( int x = 0; x < englishChar.length; x++)
        {
            System.out.println("Checking word: " + englishChar[x]);
            for (int index = 0; index < english.length; index++)
            {
                System.out.println("Comparing equality of: " + englishChar[x] + " and " + english[index] + " they are " + (englishChar [ x ].equals( english[index]) ? " equal" :  " not equal"));
                if( englishChar [ x ].equals( english[index]))
                {
                    System.out.print(morse[index] + " "); // Display Morse Code array character at matching index position to show translation
                }

            }
        }

    }

}

}

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