• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with translating morse code  RSS feed

 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently working on a program that is supposed to translate morse code and then display the result and I'm stuck, my program has tons of errors when I try to compile any help is appreciated
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's helpful if you tell is what the errors are, but here are a couple of problems I can see:

- On line 5 you refer to Input. That might not be an error if you have a class of that name, and it has a static method called getString, but you haven't told us about that.

- On line 13 you refer to an input variable that isn't declared anywhere. Is that meant to be str.charAt(k)?

 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:It's helpful if you tell is what the errors are, but here are a couple of problems I can see:

- On line 5 you refer to Input. That might not be an error if you have a class of that name, and it has a static method called getString, but you haven't told us about that.

- On line 13 you refer to an input variable that isn't declared anywhere. Is that meant to be str.charAt(k)?



Well i do have a class of that name and that is how I get the user input. Well I'll post a screenshot of the errors
error.png
[Thumbnail for error.png]
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a good idea to start with the first error and then recompile. Does your first error look like this?

Assignment10.java:7: error: <identifier> expected
String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","...
.","..",".---","-.-",".-..","--","-.","---".".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--..","|"};

^


When you have a long messy line like that, try formatting it:



If you keep doing that, you'll see the problem. Fix it, recompile, then work on the next error (at the top).
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute's right. In particular, look carefully at your commas.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something very wrong with having parallel arrays. They are error‑prone. There must be a simpler way to do it.

One suggestion: you know that chars represent letters, don't you? As all beginners know. But that is totally wrong. A char is a number. So you can do arithmetic on it. You can work out the index for the Morse code like that and print the code out. What you do about spaces I am not sure, but I suspect that printing | isn't quite it.

Please go back with the button and break those long lines; if you don't have a posh big screen, they are very difficult to read.
 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is something very wrong with having parallel arrays. They are error‑prone. There must be a simpler way to do it.

One suggestion: you know that chars represent letters, don't you? As all beginners know. But that is totally wrong. A char is a number. So you can do arithmetic on it. You can work out the index for the Morse code like that and print the code out. What you do about spaces I am not sure, but I suspect that printing | isn't quite it.

Please go back with the button and break those long lines; if you don't have a posh big screen, they are very difficult to read.

"|" was supposed to represent a space between words
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought you used a short space between letters and a long space between words. If you print the letters without intervening spaces, the output will be incomprehensible.
 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I've fixed the 50 errors I had .... I missed 1 comma. 1! 1 comma caused 50 errors lol. It's compiling and I'm pretty sure it spits out the right translation (not fluent in morse code so I'm not 100% certain)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deron Brown wrote:Well I've fixed the 50 errors I had .... I missed 1 comma. 1! 1 comma caused 50 errors lol...

So hopefully you've learned an important lesson: COMPILE OFTEN.

My rule of thumb is:
  • After every 10 lines you write.
  • After every method you complete.
  • and those should be 'AND'ed together.

    PS: I've broken up those enormous lines that both you and Knute wrote, because they were making the thread very difficult to read.

    Winston
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Deron Brown wrote:Well I've fixed the 50 errors I had .... I missed 1 comma. 1! 1 comma caused 50 errors lol. It's compiling and I'm pretty sure it spits out the right translation (not fluent in morse code so I'm not 100% certain)


    And it is *not* 50 errors... it is one error (or maybe two errors). Keep in mind that the compiler has to guess at where the good code starts again after it finds an error. And sometimes, it just messes up. This can cause other errors to not be detected, or cause false positives where errors are detected that doesn't exist.

    This is also why you should *always* target the first few errors and recompile. The accuracy of the errors are not equal -- they get worse further down the list.

    So, it is basically two errors -- the first is the comma, and the second is the compiler messing up...

    Henry
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!