LightFlicker
LightFlicker

Reputation: 67

Trouble returning a String from a nested for loop

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.

Upvotes: 1

Views: 141

Answers (1)

Eran
Eran

Reputation: 394146

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

Upvotes: 5

Related Questions