Q:

Translating strings to characters using the split method

My program is to translate an input from English to Morse Code and vice versa. First, it prompts the user as to whether they will be typing in Morse Code or English. After that, it translates one to the other. I'm working on how to translate Morse Code to English. I am using this array containing letters in Morse:
String[] codes = {".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. ", ".---- ", "..--- ", "...-- ", "....- ", "..... ", "-.... ", "--... ", "---.. ", "----. ", "----- ", "| "};

The user's input must be formatted in a specific way. When typing in Morse Code, each group of characters that represent a letter must be separated by a space. Each full word must be separated by a straight line ("|"). Punctuation and capitalization is not necessary.
Here is an example to clarify:
Input:
 - .... .. ... | .. ... | .- | - . ... -

Output: 
THIS IS A TEST

My code is as follows:
import javax.swing.JOptionPane;

public class ProjectOne
{
    public static void main (String[] args)
    {
        String decision = getString ("Will you be typing in English or Morse Code?");
        String english = "english";
        String morse = "morse";
        String[] codes = {".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. ", ".---- ", "..--- ", "...-- ", "....- ", "..... ", "-.... ", "--... ", "---.. ", "----. ", "----- ", "| "};
        char[] letters = {'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', ' '};

        if (decision.equalsIgnoreCase(morse))
        {
            for (int a = 0; a < codes.length; a++)
            {
                codes[a] = codes[a].trim();
            }

            String input = getString ("Enter a phrase to be translated to English:");
            String[] parts = input.split(" ");

            for (int n = 0; n < parts.length; n++)
            {
                for (int i = 0; i < parts.length; ++i)
                {
                    if (parts[n].equals(codes[i]))
                    {
                        System.out.print(letters[i]);
                    }
                }
            }
        }
        else if (decision.equalsIgnoreCase(english))
        {
            //Translates English to Morse Code
        }
        else
        {
            System.out.println("Invalid Input - Type 'English' or 'Morse'");
        }
    }

    public static String getString(String paramString)
    {
        String str = JOptionPane.showInputDialog(paramString);
        return str;
    }
}

My problem is that the translation to English doesn't appear correctly at all. When trying the example I provided above, the output reads "HIIAE". My intent was to first trim the extra space off of the end of each string in the array containing the Morse Code characters, then split the user's input by the space in between each letter and match the parts up, then finally display the corresponding character in the array with the English Alphabet. What can I do to get my program to translate the user's input in Morse Code into English?

A:

As I said in my comment above, the problem is a bounds error. Your inner loop is supposed to loop over every index in your codes array, but instead you're using parts.length as the upper bound. Since your sample input only has 14 segments, the inner loop only tries to match against the first 14 letters of the alphabet. That means any letter after N isn't going to show up. If you filter out all letters after N, then THIS IS A TEST becomes HIIAE. Look familiar?
This version works correctly:
        for (int n = 0; n < parts.length; n++)
        {
            for (int i = 0; i < codes.length; ++i)
            { //                ^ Fixed the loop bound here
                if (parts[n].equals(codes[i]))
                {
                    System.out.print(letters[i]);
                    break; // Quit inner loop once you find a match
                }
            }
        }
        System.out.println(); // Add a trailing newline

However, this is a really inefficient way of finding the matches, both in terms of the time complexity (how long it takes to find the match) and the complexity of the code itself. Rather than looping over the entire array for each morse code segment to try and find a match, you could just shove both the arrays in a HashMap and do the lookup much more simply:
// Initialize the map for Morse code lookup
Map<String, Character> lookup = new HashMap<>();
for (int i=0; i<codes.length; i++) lookup.put(codes[i], letters[i]);

// Build the result string by looking up each Morse code segment in the map
StringBuilder result = new StringBuilder();
for (int i=0; i<parts.length; i++) result.append(lookup.get(parts[i]));
System.out.println(result);

