Search code examples
javamorse-code

Morse code translating morse into language java


I have a bit of an issue with my morse code, especificly I want it to print out a single question mark when the user inputs more than just morse code, i.e .-asd, the output should be "?", currently i have it just skipping to the next line,

import java.util.Scanner;

public class Morse {
     static String[] MORSE = {
        ".-" ,"-...","-.-.","-.." ,"." , //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
    };
     public static void main(String[] args){

     Scanner input = new Scanner(System.in);

     String line;
     while(!(line = input.nextLine().toUpperCase()).equals("*")){
         String[] words = line.split(" ");
         String output = "";

         for(int i = 0; i< words.length; ++i){
             if(words[i].length() == 0){
                 output += " ";
                 continue;
             }
             if(words[i].charAt(0) == '-' || words[i].charAt(0) == '.'){ //if it begins as morse code
                 for (int d = 0; d < MORSE.length; ++d) {
                     if(words[i].equals(MORSE[d]))
                         output += (char)('A'+d);
                 } //i wanted here to make a condition where if it starts as morse and follows with other things other than morse print out a single "?".
             } else System.out.print("?") //prints ("?") if its not morse code

Solution

  • Make RegEx pattern at the top outside any loops:

    java.util.regex.Pattern regExPattern= java.util.regex.Pattern.compile("([\\.,\\-]){1,}");
    

    then inside loop instead of

       if(words[i].charAt(0) == '-' || words[i].charAt(0) == '.')
    

    your if will be:

            if(regExPattern.matcher(words[i]).matches()) {
               //it is MORSE code - translate it
               ...
            } else {
               // it is something else 
               System.out.print("?")
             }
    

    BTW: there are more better solutions rather than using arrays. Check code in my answer there: Morse Code Translator

    Just wondering from where that assignment came :-) ? It is second similar question in last 3 days...

    UPD: only for loops (also it eliminates non-morse .- sequences)

    for (int i = 0; i < words.length; ++i) {
            boolean gotMorse = false;
            for (int d = 0; d < MORSE.length; d++) {
                if (MORSE[d].equals(words[i])) {
                    // code found.
                    output += (char) ('A' + d);
                    gotMorse = true;
                    break;
                }
            }
            if (!gotMorse) {
                output += "?";
                // or System.out.print("?"); if you do not need ? in the output
            }
    
        }
    

    Q: how do you plan to deal with special MORSE (e.g. ...--- ) or it is not in assignement?