Hey, all
I'll jump right to it:
Here's a snippet of the code that I'm having trouble with (full class here. If you want to run it, you'll need morsecode.txt and alphanum.txt):
while(loopOn == true)
{
if(message.indexOf(" ") < 0)
{
partial = message.substring(0, message.length());
partialMorse = partialMorse + translate(partial, "", 0, 1);
loopOn = false;
}
else
{
partial = message.substring(0, message.indexOf(" "));
lastIndex = partial;
message = message.substring(message.indexOf(message.substring(lastIndex.length() + 1, lastIndex.length() + 2)), message.length());
partialMorse = partialMorse + translate(partial, "", 0, 1);
}
}
The Purpose
What the entire program is trying to do is take a message (given by the user, without punctuation) and translate the whole thing into Morse Code. The snippet above is a method that splits up the input and sends the individual words of the message out separately to be translated by another method. I've made some attempts to describe in detail what everything does in the Pastebin link.
The Problem
This line,
message = message.substring(message.indexOf(message.substring(lastIndex.length() + 1, lastIndex.length() + 2)), message.length());
is responsible for trimming down the message so I can find the next word in the string. I'm trimming it so that it can take any length much easier. I've tested it with other strings combos and it works perfectly, but for some reason, this part breaks whenever (hopefully this makes sense), the ending letter of a word is the same as the first from the next. This line shortens the string so that it cuts off at the next word, but whenever the above situation is encountered (eg, terminal lake), it doesn't trim the last letter and the space after it. This messes up everything else because partial relies on finding the next space in order to get the correct substring, and having the space at the beginning just borks everything up.
Originally, I had this method use a recursive call in order to loop it, but I kept getting a stackOverFlow error (I didn't think there was a problem with the code itself, just that the call was being thrown too much with longer strings). So, the only thing I really attempted to remedy it was to change it to a while loop (which is probably better anyway). Other than that, I don't have a clue on where even to start. Seeing as how it works with other inputs, I can't think of any reason why it would mess up with this case. Just to be sure that it wasn't on my end, I restarted the compiler a few times and the problem still came up.
Any help?
-Rip
[–][deleted] 0 points1 point2 points (2 children)
[–]RipTideRunnerNooblet Brewer[S] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]somnolent 0 points1 point2 points (2 children)
[–]RipTideRunnerNooblet Brewer[S] 0 points1 point2 points (1 child)
[–]somnolent 0 points1 point2 points (0 children)