Need assistance with java morse code translator -


i have java translator works when translate english morse not morse english. if tell me need make translate morse great. when go morse english after enter morse code ends program instead of giving me translation.

here 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 = input.getstring ( "please enter mc if want translate morse code english, or eng if want translate english morse code" ); if (a.equals("mc"))     {         string b = input.getstring ("please enter sentence in morse code. separate each letter/digit single space , delimit multiple words | .");              string[] words = b.split("|");         (string word: words )         {             string[] characters = word.split(" ");             (string character: characters)              {                 if (character.isempty()) { continue; }         (int m = 0; m < b.length(); m++)                 {                     if (character.equals("inputmorsecode[m]"))                             system.out.print(english[ m ]);                     }                 }             system.out.print(" ");             }         } else if (a.equals("eng"))     {         string c = input.getstring ( "please enter sentence in english, , separate each word blank space." );          c = c.tolowercase ();          ( int x = 0; x < english.length; x++ )         {             ( int y = 0; y < c.length (); y++ )             {                 if ( english [ x ] == c.charat ( y ) )                  system.out.print ( morse [ x ] + "  " );               }          }       }      else     {        system.out.println ( "invalid input" );      }  } } 

first of all, change this

    (int m = 0; m < b.length(); m++)             {                 if (character.equals("inputmorsecode[m]"))                         system.out.print(english[ m ]);                 }   

to

    (int m = 0; m < morse.length; m++)             {                 if (character.equals(morse[m]))                         system.out.print(english[m]);                 }  

since supposed search morse letter in morse array.

that said, code more efficient if create map<string,character> , map<character,string> map morse strings english characters , vice versa. replace morse , english arrays, , allow find mapping of english or morse letter in constant time.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -