Morse Code to English, (If statement?)

478 views Asked by At

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;
                   }
                }


            }

        } 



}
    }}
3

There are 3 answers

1
Dima On BEST ANSWER

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;
       }
    }
}
0
risto On

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.

1
arcy On

I think you're after java.util.StringTokenizer