• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Morse Code to English

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
need to convert morse code into English but without using arrays or maps
need help!!
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your question? Is the code failing to compile, is it not behaving the way you want it to etc?
 
Marshal
Posts: 70234
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Always use the code button; I have edited your code with it and doesn't it look better
As you have been told, you need to tell us what is going wrong otherwise we don't know what to suggest.
 
Saloon Keeper
Posts: 2662
130
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saad Salman wrote:... need to convert morse code into English but without using arrays or maps



Why ?
 
Campbell Ritchie
Marshal
Posts: 70234
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:. . . Why ?

Because some teachers don't explain their requirements very well. The idea is to demonstrate that the students can write if‑elses. So you tell them not to use Maps, which would usually be regarded as poor programming.

I would have used a switch statement myself.
 
salvin francis
Saloon Keeper
Posts: 2662
130
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I would have used a switch statement myself.


I would have used a HashMap
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:

Campbell Ritchie wrote:I would have used a switch statement myself.


I would have used a HashMap


The requirements specifically prohibit the use of Map implementations.
 
Campbell Ritchie
Marshal
Posts: 70234
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course a Map is the best solution. So would be a MorseCharacter class, but the OP is being tested on writing if elses.I used 131071 for hash because it is a Mersenne prime. The %0x20 bit means you get the same hash code for 'A' and for 'a'.
 
Saloon Keeper
Posts: 12253
259
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have used an enum.
 
Stephan van Hulst
Saloon Keeper
Posts: 12253
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I used 131071 for hash because it is a Mersenne prime. The %0x20 bit means you get the same hash code for 'A' and for 'a'.


My personal preference would be to store the letter in canonical form (for instance, in uppercase), and using Objects.hash(code, letter) to generate the hash code.
 
salvin francis
Saloon Keeper
Posts: 2662
130
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I would have used an enum.


I am a bit curious about this in enums .. were you still going to use switch cases ?
I was thinking of its in lines of valueOf() method, but then his values were dots and dashes.

maybe like this:


 
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I build a trinary tree whose branches are "dot", "dash" and "terminate", does that count as a map?

This, BTW is why I flunk employment programming aptitude tests. It's never one of the "right" answers.
 
Stephan van Hulst
Saloon Keeper
Posts: 12253
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the assignment is to not use any arrays at all, then yes, I would work with a switch statement. If not, then the values() method can be used. The general outline would be something like this:
 
Campbell Ritchie
Marshal
Posts: 70234
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:So if I build a trinary tree whose branches are "dot", "dash" and "terminate", does that count as a map? . . .

Surely that is a Huffman code
 
Campbell Ritchie
Marshal
Posts: 70234
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, Stephan, you are right about Objects#hash.
I don't think there is any chance of a null String getting into my class, is there?
 
Tim Holloway
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finite State Machine? I've used Antlr since it was still known as the Purdue Compiler Construction Toolset.

I know, I know I don't get the job.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saad.

Please don't ask Java questions in Private Messages. Post the question in the main thread so everyone can see and contribute.

For the benefit of others here, Saad would like to know why the code works for single characters but not for multiple characters.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason the code only works for single characters is because you are comparing the entire input String against the code for each character. If the string does not exactly match the code for a single character only then it won't do anything.

You are going to have to find an approach to take your string and see if it starts with the code for a character. You will then need to discard the part of the input used and repeat for the next character.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and your code for A appears to be the same as your code for J. I think you have made an error somewhere.
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic