1

I looked for other examples on StackOverflow that might answer my question, but none of the answers in the other questions focused on a nested for loop. I'm making a Morse code translator, and the program itself works fine if I do this:

public static void StringtoMorse(String str){
    char 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', ' '};
    String MorseCode [] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "|"};


    for (int i = 0; i < str.length(); i ++){
        for (int j = 0; j < Alphabet.length; j ++){
            if (str.charAt(i) == Alphabet[j]){
                System.out.print(MorseCode[j] + " ");
            }
        }
    }


}

But I want to make the method so that it returns the String of (MorseCode[j] + " "). So this is how I edited my method to do that:

public static String StringtoMorse(String str){
    char 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', ' '};
    String MorseCode [] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "|"};


    for (int i = 0; i < str.length(); i ++){
        for (int j = 0; j < Alphabet.length; j ++){
            if (str.charAt(i) == Alphabet[j]){
                return(MorseCode[j] + " ");
            }
        }
    }


}

But that results in a compile error. The error says "This method must return a result of type String", but I thought the (MorseCode[j] + " ") is a string type. I know that MorseCode[j] has to be a String because I've defined MorseCode as a String array.

If I use the first method (With the System.out.println() method), it properly returns the result.

1 Answer 1

5

Your method must have a return statement after the for loops too, in case the for loops are never entered or the input String contains a character that doesn't match any of the Alphabet (in which case the if condition is never true).

public static String StringtoMorse(String str){
    char 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', ' '};
    String MorseCode [] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "|"};


    for (int i = 0; i < str.length(); i ++){
        for (int j = 0; j < Alphabet.length; j ++){
            if (str.charAt(i) == Alphabet[j]){
                return(MorseCode[j] + " ");
            }
        }
    }
    return null;
}

However, I'm not sure your method does what you want it to do, since it would just return the Morse code of the first character in the input String. You probably want to translate the entire input String to Morse.

Here's an alternative implementation that would convert the entire input String to Morse :

public static String StringtoMorse(String str){
    char 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', ' '};
    String MorseCode [] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "|"};

    StringBuilder morse = new StringBuilder();
    for (int i = 0; i < str.length(); i ++){
        for (int j = 0; j < Alphabet.length; j ++){
            if (str.charAt(i) == Alphabet[j]){
                morse.append(MorseCode[j]);
                morse.append(' ');
            }
        }
    }
    return morse.toString();
}

Of course, you can make it more efficient and eliminate the inner loop :

public static String StringtoMorse(String str){

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

    StringBuilder morse = new StringBuilder();
    for (int i = 0; i < str.length(); i ++){
        if (str.charAt(i) >= 'a' and str.charAt(i) <= 'z'){
            morse.append(MorseCode[str.charAt(i)-'a']);
            morse.append(' ');
        } else if (str.charAt(i) == ' ') {
            morse.append("| ");
        }
    }
    return morse.toString();
}
Sign up to request clarification or add additional context in comments.

8 Comments

Well the code returns the Morse code of each character in the input String. It works in the output. But thank you for the feedback. What exactly does the return null method do?
@LightFlicker The code would return just the Morse code of the first character of the input String that appears in the Alphabet array. The return null returns a default value in case there is no Morse code to return (you can also return an empty String if your prefer) - for example, if str is an empty String.
In your answer, you said morse.append(MorseCode[str.charAt(i)-'a']); But str.charAt(i)-'a' would return a string, right? So how can that be the index of a term in MorseCode?
@LightFlicker str.charAt(i)-'a' is a subtraction of two chars, which is a char. A char can be an index of an array.
I thought indices could only be integer values.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.