• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why doesn't this program translate morse code correctly?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I'm having some trouble with getting this program to read an input of morse code and then produce an output of English. When typing in morse code for the phrase 'the string', the output looks something like this:
- .... .
t
h
... - .-. .. -. --.
s
t
r
i
n
The English-->Morse works just fine.



Any help would be greatly appreciated.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paige An wrote:I'm having some trouble with getting this program to read an input of morse code and then produce an output of English. When typing in morse code for the phrase 'the string', the output looks something like this:
...
Any help would be greatly appreciated.


OK, well clearly your program isn't processing the "last letter" it receives; and the reason is that
while(morseWord.indexOf(" ") > 0) { ...
test.

As soon as it returns false, you simply drop out of the loop, when in fact you almost certainly have another letter to process.

One possibility would be to make that loop a do...while loop; but even easier is probably to use String.split(). It was created for precisely the kind of processing you're doing, and you could use it for both "words" and "letters".

Another point: You have - quite rightly - split out your translation to Morse into a separate method, but you still have all your "translate to English" code in main(). Why? If you had a toEnglish() method, your main() could be as simple as:You may also notice that I use the first input to create a boolean. It's not required, but I think you'll agree that it makes the code more readable. Why? Because Strings should ONLY be used to contain text; and in this case your 'answer' field is redundant because it simply contains the answer to the question "Do you want to translate to Morse?". For more info, have a look at the StringsAreBad page.

Also:
1. You don't have to create a NEW Scanner every time you get new input. The 'scan' creation at line 31 is therefore redundant.
2. What do you do if the user enters something invalid? Right now, it looks like you'll just end up spitting out blanks, which isn't very helpful. Good programmers have a strategy for what to do when things go wrong.

HIH

Winston

PS: I've broken up your lines as best I can because they are FAR TOO LONG (mostly because your indenting is excessive). Please read the link before you post any more code. Thanks.
 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I know my code isn't ideal but I'm only a beginner.

And when a user enters invalid data, the program just stops.

Doesn't String.split create an array? Also, I tried a do-while loop without much success:

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you expect the Morse code input to be? From the code it looks like it would have to have a pipe (|) and a space. Why?

 
Paige An
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm required to have it. I'm not sure why either.

The morse code input would look something like this:

-. | ....
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paige An wrote:Thanks for the reply. I know my code isn't ideal but I'm only a beginner.


OK, but my question was: When you've already correctly created one method, why did you stop there?

For future reference:
1. More methods are almost always good.
2. In general, the smaller your main() method is, the better.

And when a user enters invalid data, the program just stops.


It does? What makes it do that?

Try entering "yes" + "@#&?" and see what happens.

Doesn't String.split create an array?


Yes.

Also, I tried a do-while loop without much success:


That's because you're trying to get it to work with the same logic you've already written, and that won't work. You'll have to change things a bit.

My advice:
1. StopCoding (←click)
2. Concentrate on that loop, and write out IN ENGLISH (or your native language) exactly what it has to do. Every single step.

And don't try to code another line of Java until you've done that.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paige An wrote:I'm required to have it. I'm not sure why either.


Probably to separate words, since you already use space to separate Morse "letters".

Winston
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you've started coding again, do look into String.split. It's very useful in breaking up strings.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic