0

The English to morse code part works fine, But the Morse code to English doesn't...I keep getting E's and T's , Is there a way to make so that if there's a space in between the Morse code letters it would know theyre different letters instead of having every "." = E and every "-" =T

I'm trying to get my code to check for spaces when its going from morse code to english, its not working properly.

Heres my full code:

import java.util.Scanner;

public class MorseCode {
    public static void main(String [] args){


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

    //Decide Either English to Morse or Morse to English



    System.out.println("For English-->Morse (Press 'E' )\n"
                    +  "For Mosrse-->English (Press 'M')");

    Scanner Input = new Scanner(System.in);
    String Response = Input.next();

    //For invalid Input
    while(!(Response.equalsIgnoreCase("E") || Response.equalsIgnoreCase("M"))){ 
        System.out.println("Invalid input, Please type in E or M");
        Response = Input.next();
    }
    //English to Morse-Code
    if(Response.equalsIgnoreCase("E")){

        System.out.println("Type in English");
        Input.nextLine();
        String Ephrase = Input.nextLine();
        Ephrase = Ephrase.toUpperCase();

        for(int count = 0; count < Ephrase.length(); count++ )
        { 
            for(int index = 0; index < Alphabet.length; index++) 
            { 
                //Matches English to Morse and Prints
                if(Ephrase.substring(count, (count+1)).equals(Alphabet[index]))
                    System.out.print(Morse[index] + " "); 
            } 
        } 


    }
    //Morse-Code to English
    else if(Response.equalsIgnoreCase("M")){

        System.out.println("Type in MorseCode");
        Input.nextLine();
        String Mphrase = Input.next();

        for( int count = 0; count < Mphrase.length(); count++  )
        { 
            for (Scanner s = new Scanner(Mphrase); s.hasNext();) {

                String letter = s.next();

                for(int index = 0; index < Morse.length; index++) {

                    if(letter.equals(Morse[index])) { 

                        System.out.println(Alphabet[index]);
                        break;
                   }
                }


            }

        } 



}
    }}
AAA
  • 3
  • 2

3 Answers3

0

I think the simplest thing is to create a map:

int totalLetters = 26;
HashMap<String, String> englishToMorse = new HashMap<String, String>(totalLetters);
HashMap<String, String> morseToEnglish = new HashMap<String, String>(totalLetters);

for (int i = 0; i < Alphabet.length; i++) {
    englishToMorse.put(Alphabet[i], Morse[i]);
    morseToEnglish.put(Morse[i], Alphabet[i]);
}

Then when you need to translate from one to the other, split up the words in the sentence, and for each word, check the appropriate dictionary/HashMap.

risto
  • 1,274
  • 9
  • 11
0

Instead of scanning your morse phrase one character at a time, use a StringTokenizer or a Scanner to split it at whitespace.

for (Scanner s = new Scanner(Mphrase); s.hasNext();) {
    String letter = s.next();
    for(int index = 0; index < Morse.length; index++) {
       if(letter.equals(Morse[index])) { 
          System.out.println(Aplphabet[index]);
          break;
       }
    }
}
Dima
  • 39,570
  • 6
  • 44
  • 70
  • I got different output using that, I think it has something to do with my brackets – AAA Dec 06 '14 at 03:27
0

I think you're after java.util.StringTokenizer

arcy
  • 12,845
  • 12
  • 58
  • 103
  • I think this would be better as a comment, unless you'd like to expand on this answer and demonstrate how to implement your suggestion. – Paul Richter Dec 06 '14 at 02:54