0

I'm trying to do Morse Code Decoder on Processing 3. I have codes for Morse Code Encoder. But I'm struggling about convert encoder to decoder.

Also I want to enter Morse Code with clicking, but it's going to be next step after the conversion.

I tried to change inputs and outputs and alphabets to the morse, but in the last section of the code, i stucked.

Here is the original encoder : https://www.openprocessing.org/sketch/134812/

Here is my modified decoder code:

String textInput;
String textOutput;

void setup() {
  textInput = decodeMorseCode(textInput);
  textOutput = "";

   println("Morse code: " + textInput);
  println("Text output: " + textOutput);

}

void draw() {
}

String decodeMorseCode(String in_string) {

  String MorseCodeInput = in_string.toLowerCase();
  String TextOutput =  new String();

  String[] MorseCodeArray = {
    "._", "_...", "_._.", "_..", ".", ".._.", "__.", "....", "..", ".___", "_._", "._..", "__", 
    "_.", "___", ".__.", "__._", "._.", "...", "_", ".._", "..._", ".__", "_.._", "_.__", "__.."
  };

  String[] AlphabetArray = {
    "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"
  };


  for (int i=0; i<TextInput.length(); i++) {
    for (int j=0; j<AlphabetArray.length; j++) {
      if (String.valueOf(TextInput.charAt(i)).equals(AlphabetArray[j])) {
        MorseCode += MorseCodeArray[j] + " " ;
      }
    }
  }
  return MorseCode;
}

I'm getting an error on the line "for (int i=0; i

If it's going to be work properly it should decode morse to text.

1 Answers1

0

I'll gladly help you through this, but first I must say that this community isn't about teaching the basics. I suggest you visit this website and follow their instructions to learn more about Processing and programming in general.

I strongly do.


Here's some basic things you may or may not know about Processing:

Processing has 2 very important methods: setup() and draw().

When you run your program, Processing first read all the global variables. Then it reads setup(), and then draw(). The last one is a controlled loop which run a fixed number of times per second. It's where most of the magic happens.

setup() is mostly used to "prepare" everything so the draw() loop is ready to go.

I took a look at the encoder's code. It takes an input (letters, words, etc) and gives an output (morse code). An encoder should be the opposite: you give the program some morse code, and it gives you a translation.

Now, to the problem at hand:


Remember when I said "Don't just say that you got an error, tell us what the error says". That helps a lot. I ran your code in Processing, and it wasn't the problem I though it was first (well, the thing I though was a problem, but not the one which crashes your code at this point).

Here are the problems which crash your code, and how to deal with them. Reading this will hopefully help you learn about Processing, but, really, do the tutorials.

  1. The variable TextInput doesn't exist. Instead you named it MorseCodeInput, but didn't change it in the code. There are 2 different easy fix to this, choose one:

    • You can rename the variable MorseCodeInput as TextInput.
    • You can rename every instance of TextInput as MorseCodeInput in the decodeMorseCode method.
  2. Where you should use TextOutput, instead you used MorseCode. You have to rename those as TextOutput at these two places:

    • MorseCode += MorseCodeArray[j] + " " ; => TextOutput += MorseCodeArray[j] + " " ;
    • return MorseCode; => return TextOutput;
  3. In the setup() method, initialize the input to some actual morse code.

    • textInput = "_._. ___ _. __. ._. ._ _ .._ ._.. ._ _ .. ___ _. ..."; should do it.
  4. The textOutput variable should be the result of the translation. In the setup() method, use it this way:

    • textOutput = decodeMorseCode(textInput);
  5. Yay! No crash! But no output either? Why? There are two reasons:

    • First because the algorithm is looking for letters and we gave it dots and lines.
    • Second because it does it one character at a time, which makes no sense for morse code where a letter is several dots and lines (which are one character each).

So... That's where you stand. The code is running, but you still have several issues to address. I'll give you one of these, because it'll be kinda hard to figure out for a beginner.

Here's how to get an array of Strings for your morse code. In the decodeMorseCode() function, initialize the TextInput variable like this:

String[] TextInput = split(in_string, ' ');

And also those lines:

for (int i=0; i<TextInput.length; i++) {
// ...
if (String.valueOf(TextInput[i]).equals(AlphabetArray[j])) {

You will still have no output, but now you only have to figure out how to compare the morse code to the morse code array to get the appropriate letter, and not the other way around. Let me know if you try and fail, and I'll give you a hand.

Have fun.

laancelot
  • 3,138
  • 2
  • 14
  • 21