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.
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.
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'.
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.
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.
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:
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.