• Post Reply Bookmark Topic Watch Topic
  • New Topic

Morse to English

 
Bobby Morrison
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I am having a little trouble converting Morse to English in Java.

I have this code based on English to Morse but cannot work out how to make it Morse to English.

If anyone can point out where I've gone wrong, I would very much appreciate it.

Thanks.

 
Campbell Ritchie
Marshal
Posts: 53171
124
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I would set up my array differently.I made that array initialiser from your code by repeated use of the delete key (mostly). Array initialisers have the advantage that the ize of the array is exactly the same as the initialiser, so you get exactly 35 elements by adding the 26 letters of the alphabet to the ten digits. Or you might have if you had remembered x
You shou‍ld move most of the code out of the main method.
Never use while (myScanner.hasNext()) ... when you are looking at System.in, and never close such a Scanner. System.in always has a “next” implicitly, so that loop will go on for ever. Then when you close the Scanner, you close System.in and can never open it. Maybe you would do better to use the nextLine method (beware of nextLine after nextAnythingElse) and split it into separate tokens with line.split(" ").
Beware: I put one space there, which means you will have to separate successive words by single spaces. Otherwise you will get empty Strings (="") in your resultant String[]. You can use regular expressions to solve that problem, but regular expressions may be a bit advanced for you at present.

I would start again from scratch: what about a MorseAndEnglish class? Actually it doesn't convert Morse code to English. It converts Morse Code to any language using the 26‑letter alphabet which we use. You can have something like:-Now you can put those into an array and see whether the letter or the code equal whatever you have. Once you have a MorseAndWriting object, you can look up both the letter and the code from it.
 
Henry Wong
author
Sheriff
Posts: 22629
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobby Morrison wrote:
I have this code based on English to Morse but cannot work out how to make it Morse to English.


I think you may have based it *too* much on the English to Morse Java code. If you look at the original Java code, you will see that the data types used for a single Morse code are not characters -- they are strings of dots and dashes characters. The character type is not able to hold a single Morse code for an English character.

Likewise, you can't use the same Java code for breaking up the user input. Since Morse code are not single characters, you can't break it up with the charAt() method, as you will be breaking individual Morse code into pieces.

Quite frankly, I am not sure how much Java code can be repurposed here. Going from English to Morse is a "one to many" algorithm, which is much different than a "many to one" algorithm. It may be better to examine/work out the algorithm, than to force fit Java code.

Henry


PS... BTW, welcome to the ranch !!

 
Knute Snortum
Sheriff
Posts: 3329
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java convention is that variables start with lowercase letters.  Also this: is better than this: You should program to the interface when possible, not the implementation.  List is the interface.  ArrayList is one implementation.
 
Junilu Lacar
Marshal
Posts: 10409
125
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line won't even compile:

That would give you an "Invalid character constant" compile-time error

I'm assuming that the Morse text uses spaces to separate one Morse code letter from the next. If you use a Scanner to go through the Morse code text, each letter in Morse will be a token that you'd read with Scanner.next(). The easiest way to translate that would be to use a Map<String, Character> or Map<String, String> where the key is the Morse code for one letter and the value is the equivalent English letter. Look for examples of using a Map and for using a Scanner to tokenize a String. An even simpler way to break your Morse code text into its component Morse code letters is to use the String.split() method, splitting the string up on spaces.
 
Junilu Lacar
Marshal
Posts: 10409
125
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The translator on this page http://morsecode.scphillips.com/translator.html uses spaces to separate Morse letters in a word and it uses "/" to separate words from each other. Does your morse.txt follow the same convention or does it only use spaces as delimiters?
 
Campbell Ritchie
Marshal
Posts: 53171
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the mariners' convention was short space between successive letters and long space between words; you can mimic that with a double space, in which case the "" returned between those spaces might be useful as a word separator.
 
Junilu Lacar
Marshal
Posts: 10409
125
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I thought the mariners' convention was short space between successive letters and long space between words; you can mimic that with a double space, in which case the "" returned between those spaces might be useful as a word separator.

A longer pause to indicate a separate word is fine if the translator was getting auditory input. If your translator is a machine that uses text input, a Scanner can work if you use a single space as the delimiter.  It would be easier for a human to eyeball the morse text and see "/" instead of "  " separating words though. It's a little more difficult to pick out the difference between one space and two spaces.  Either way can work for a program though, you just need to specify the appropriate pattern to use for the delimiter.
 
Junilu Lacar
Marshal
Posts: 10409
125
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was actually a fun little program to write. I even got mine playing the message out as a series of audible dit-dahs like that site I linked to earlier. I used the princeton.edu StdAudio class and a slightly modified Tone class from the same library. Had to fiddle a bit with the durations to use for dits and dahs but got it to pretty much match that other translator's pace for the most part.
 
Bobby Morrison
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your replies so far.

I'll be going through all this over the weekend and hopefully get things up and running.

I have came across the "Switch" and "Hashmap" whilst trying to do this but I'm really trying to have a program that reads from an "Array" and a "Text File". Every time it's the "if (user.charAt(i) == '.-')" that is slipping me up.

Once again thanks, I'll post my results.

P.S, I'm still very much a beginner with this.
 
Campbell Ritchie
Marshal
Posts: 53171
124
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, switch and Maps are each an alternative technique to using an array. Since Java7 that has become easier because Java7 permits Strings as labels after case.

You cannot implement a Morse code sequence as a char, but only as a String. Look at my examples with the MorseAndWriting class. Find out about the StringBuilder class or the StringJoiner class for putting your messages together for the output.
 
Junilu Lacar
Marshal
Posts: 10409
125
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a twist to this program, if you're looking for a challenge: write a toPhoneticMorse() method that takes either English text or the Morse code text.  The method should detect whether the text it's passed is Morse code or English text. Then, it should output the phonetic equivalent of the Morse code. For example:

One solution would be to hard code the phonetic equivalents of each letter as defined in this page: http://morsecode.scphillips.com/morse.html but that's a little too easy. That would also be pretty much Kobayashi Maru'ing it.

A programmatic solution is not that hard. The "-" is straightforward: just output "dah". The dots are not as straightforward because you can output either "di" or "dit", depending on where in the code sequence the dot appears. If it's the last dot in a sequence, then you output "dit", otherwise, output "di"

You also have to output a dash between "dah"s, "di"s, and "dit"s to connect the parts of a code sequence.
 
Bobby Morrison
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm back and starting from scratch.

Basically what I am trying to do now is to take some Morse code from a text file using a Buffered Reader and then write it to a separate file using File Writer.

I'm gonna need to use a Split Method to separate the ",".

I'm also wanting 2 Arrays (English, Morse) to help translate the message.

But I'm stuck, already.

How do I fix this?


 
Campbell Ritchie
Marshal
Posts: 53171
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobby Morrison wrote:. . . I'm gonna need to use a Split Method to separate the ",".
Why split on commas? When Morse code was in common use, they used single space (or a silence equivalent to it) to separate successive letters and double space to separate words. If you do that and split on a single space, you will get an empty String "" between words, which you can use as a signal to add a space to your plan text.
I'm also wanting 2 Arrays (English, Morse) to help ranslate the message. . .
You don't actually. But using arrays is something you can do later. Remember that Morse code can be used to translate any writing using Latin text.
So, are you getting the code read from the files correctly? Are you splitting the code correctly?

Now, looking at your code, the first thing I see is frequent use of the keyword static. Why are you using it? If you haven't got a good reason, then any use of static is a serious mistake. And using static to prevent a compiler error is a bad reason. Why are you testing whether the file exists in line 18? Go ahead and create the readers and let the Exceptions find out what went wrong. What are you doing with the text read in lines 26 and 31? You have to record that text somehow, so the other methods can use it. That code looks complicated. What you need is this, which does the same, but you would never guess the strange‑looking syntax:-
At least you are not trying to do everything in one method. I think you need methods which do the following:-
  • 1: Read from a file.
  • 2: Identify whether you have code or clear. You can do that by reading the first non‑whitespace character.
  • 3: Method to split the text (maybe only required for code).
  • 4: Method to convert code to plain.
  • 5: Method to convert plain to code.
  • 6: Method to write result to file.
  • You may want more methods than those. I suggest you write as many of those methods as you can, probably 1 2 3 and 6, and see them working even if with dummy inputs, and then consider the actual conversions
     
    my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
    the new thread boost feature brings a LOT of attention to your favorite threads
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!