0

I just made a java morse code translator but when i convert morse to english it converts each symbol as a letter so when i as it to translate .... it gives me e e e e instead of h. So my question is how do i make it so i can translate whole words in morse code and use a | to separate words.

here is my code

public class project1 {

public static void main ( String [] args ) {

char [] 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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };

String [] morse = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." , "--." , "...." , ".." , ".---" , "-.-" , ".-.." , "--" , "-." , "---" , ".--." , "--.-" ,  ".-." , "..." , "-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." , "|" };
    String a = Input.getString ( "Please enter MC if you want to translate Morse Code into English, or Eng if you want to translate from English into Morse Code" );
if (a.equals("MC"))
    {
        String b = Input.getString ("Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | .");    

        String[] words = b.split("|");
        for (String word: words )
        {
            String[] characters = word.split(" ");
            for (String character: characters) 
            {
                if (character.isEmpty()) { continue; }
        for (int m = 0; m < morse.length; m++)
                {
                    if (character.equals(morse[m]))    
                        System.out.print(english[m]);    
                }    
            }
            System.out.print(" ");    
        }    
    }
else if (a.equals("Eng"))
    {
        String c = Input.getString ( "Please enter a sentence in English, and separate each word with a blank space." );

        c = c.toLowerCase ();

        for ( int x = 0; x < english.length; x++ )
        {
            for ( int y = 0; y < c.length (); y++ )
            {
                if ( english [ x ] == c.charAt ( y ) )

                System.out.print ( morse [ x ] + "  " );


            }

        }


    }

    else 
   {
       System.out.println ( "Invalid Input" );

    }

}
}

2 Answers2

0

A problem you're having is that your String#split(...) method is not taking into account that | is a special character with regard to regular expressions and is not splitting the String as you would expect. You need to escape this character for things to work right. So change this:

String[] words = b.split("|");

to this:

String[] words = b.split("\\|"); // escaping the pipe char

To debug and to see if things are working as expected, use a debugger or sprinkle println's in your code like:

 String[] words = b.split("\\|");

 // TODO: remove line below from finished program
 System.out.println(java.util.Arrays.toString(words)); 

As an aside, if this were my program, I'd use HashMap<String, String> to assist with my translation of English to Morse and visa-versa.

On further searching that I just performed, please check out: Why does String.split need pipe delimiter to be escaped? for background information on why the pipe char needs to be escaped.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
-1

This is your adapted code, which works correctly...

import java.util.Scanner;

public class project1 {

public static void main ( String [] args ) {

char [] 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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };

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

Scanner scanner = new Scanner(System.in);
System.out.println("Please enter MC if you want to translate Morse Code into English, or Eng if you want to translate from English into Morse Code");
String a = scanner.nextLine();

if (a.equals("MC"))
    {
        System.out.println("Please enter a sentence in Morse Code. Separate each letter/digit with a single space and delimit multiple words with a | .");    

        String b = scanner.nextLine();

        String[] words = b.split("|");
        for (String word: words )
        {
            String[] characters = word.split(" ");
            for (String character: characters) 
            {
                if (character.isEmpty()) { continue; }
        for (int m = 0; m < morse.length; m++)
                {
                    if (character.equals(morse[m]))    
                        System.out.print(english[m]);    
                }    
            }
            System.out.print(" ");    
        }    
    }
else if (a.equals("Eng"))
    {
        System.out.println("Please enter a sentence in English, and separate each word with a blank space.");
        String c = scanner.nextLine();

        c = c.toLowerCase ();

        for ( int x = 0; x < english.length; x++ )
        {
            for ( int y = 0; y < c.length (); y++ )
            {
                if ( english [ x ] == c.charAt ( y ) )

                System.out.print ( morse [ x ] + "  " );


            }

        }


    }

    else 
   {
       System.out.println ( "Invalid Input" );

    }

}
}
Jurrian Fahner
  • 349
  • 1
  • 11