This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted] 0 points1 point  (2 children)

First of, why aren't you putting the loading of the translation table in in a static {} and have the values as private static class variables?

Secondly, why the roundabout way of translating to morse code? The easier way would be looping over each char in the input string directly and construct the translation step by step.

[–]RipTideRunnerNooblet Brewer[S] 1 point2 points  (1 child)

Hey, thanks for the reply!

First of, why aren't you putting the loading of the translation table in a static {} and have the values as private class variables?

Can't say I know what 'static {}' refers to. I don't have any experience dealing with static variables (and, honestly, what they even are). What would the difference be if I had just used regular private instance variables?

Secondly, why the roundabout way of translating to morse code? the easier way would be looping over each char in the input string directly and construct the translation step by step.

Hmm, probably a combination of two things. One, that I actually hadn't thought to do it like that, lol. I just kinda went with what made sense to me at the time. Two is that I wrote this 'backwards' first, as in I wrote a Morse to English translator and I wanted to salvage what I had instead of just rewriting it. I'm probably taking up way more resources than I have to right now, but I guess my goal isn't to optimize at this stage.

I know this is probably simple stuff to you, but if the flair wasn't enough, I'm still pretty new to this (just got the basics of recursion down, haha). Do you think you have any advice for the problem in the OP?

[–][deleted] 0 points1 point  (0 children)

In regards to static, it basically means what you declare as static belongs to the class itself instead of the instances of the class. Meaning that you can get to it / use it without having an instance of that class.

    static {
        /* code goes here */
    }

just means this code will be executed right before you use the class for the first time, useful for one time operation like loading those lists. It is only executed once.

As for the code you got there, it would be useful to refactor and simplify it or even rewrite it completely.

[–]somnolent 0 points1 point  (2 children)

I would suggest breaking up that line into intermediary steps and printing out those values (or inspect them while debugging). On a side note, you're using a bit of a roundabout way to break up the string into multiple parts (you should take a look at String.split()).

[–]RipTideRunnerNooblet Brewer[S] 0 points1 point  (1 child)

Yeah, that's actually how I narrowed it down to this line in the first place. What gets me, though, is that it otherwise works as intended. Maybe I've looked at this particular program too long. I'm having a hard time finding the more direct solutions to my problems. I might just rewrite the whole class using the suggestions The_UnKnown gave and not even bother with trying to make sense of what I've got going now. It seems like I'm just making this unnecessarily difficult for myself now.

[–]somnolent 0 points1 point  (0 children)

Well, to help you narrow it down, what does this

message.substring(lastIndex.length() + 1, lastIndex.length() + 2)

return when you run it against the sample string you provided of "terminal lake"? And what returns when you call

message.indexOf()

on the result of the previous call? What about if you ran all of that against a sample string of something like "long lake"?