user2709168
user2709168

Reputation: 117

Translating inputs with for loops

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 am first tackling the translation of English to Morse Code. I am using this array containing letters in Morse Code:

String[] codes = {".- ", "-... ", "-.-. ", "-.. ", ". ", "..-. ", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ", "..- ", "...- ", ".-- ", "-..- ", "-.-- ", "--.. ", ".---- ", "..--- ", "...-- ", "....- ", "..... ", "-.... ", "--... ", "---.. ", "----. ", "----- "};

The translation to Morse Code must be presented in a certain way. Each letter in the English alphabet must, of course, be represented by its corresponding character in Morse Code. Each character must be separated by a space. However, each word must be separated by a straight line ( | ). Punctuation and capitalization is not necessary. Here is an example to clarify:

Input: This is a test.

Output: - .... .. ... | .. ... | .- | - . ... -

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

        if (decision.equalsIgnoreCase(morse))
        {
            //Translate to English here
        }
        else if (decision.equalsIgnoreCase(english))
        {
            String input = getString ("Enter a phrase to be translated to Morse Code:");
            input = input.toUpperCase();

            for (int n = 0; n < input.length(); n++)
            {
                char ch = input.charAt(n);
                for (int i = 0; i < 26; i++)
                {
                    if (ch == letters[i])
                    {
                        String newch = Character.toString(ch);
                        System.out.print(codes[i]);
                    }
                }
            }
        }
        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 Morse Code translation appears without the "|" divider in between each word. I tried putting a space character at the end of the array containing the letters of the English alphabet and a "| " string at the end of the array containing the Morse Code characters thinking that the for loop would pick it up like it would any other character, but the translation shows up with spaces in between the words anyways. What can I do to fix this problem?

Upvotes: 0

Views: 139

Answers (2)

Daniel
Daniel

Reputation: 36720

There are two problems:

1) Your codetable is invalid. codes has 37 elemets while letters has 27

2) The for-loop does not check the foll codetable: for (int i = 0; i < 26; i++) use something like for (int i = 0; i < codes.length; i++)

Upvotes: 1

Jack
Jack

Reputation: 133669

Problem is that you are using a magic number in the for loop:

for (int i = 0; i < 26; i++)

but if you add a space at the end then the size of the array changes to 27.

You could use a more maintainable solution, eg:

for (int i = 0; i < letters.length; ++i)

or even better, use a Map to store your codification. Eg:

Map<Character, String> code = new HashMap<Character, String>();
code.put('a', ".-");

Upvotes: 4

Related Questions