I’m pulling out my hair… Yes, it’s posted all over and no, can’t seem to wrap my head around my error. I get that I’m trying to deal with the nuances of characters and strings. But my output is not helping so maybe someone else can? Below is the output I’m getting and the code that gives it to me.
Input:
...
no output
This input
... ---
output
e
e
e
and this input
...|---
output
e
e
e
Only the first morse "character" (which should be an 's'), ignores rest after the pipe and/or space
public static void morseToEnglish() {
String englishArray[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
String morseArray[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".--- ", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
//String translatedText = "";
//String newEnglishChar;
System.out.println("Enter Morse, separate each letter or digit with a single space, separate words with '|' ");
Scanner input = new Scanner(System.in);
String morseSentence = input.nextLine();
String[] words = morseSentence.split("|");
for (String word: words) {
String[] morseChars = word.split(" ");
for (String morseChar : morseChars) {
if (morseChar.isEmpty()) { continue; }
for (int i = 0; i < words.length; i++) {
if (morseChar.equals(morseArray[i])) {
System.out.println(englishArray[i]);
//newEnglishChar = englishArray[i];
//translatedText = translatedText + newEnglishChar;
}
}
}
}
//System.out.println(translatedText);
}
I'm not getting any compiling errors. The code works fine, I've just told it the wrong instructions so my output is not as I'd prefer. It seems to me that Scanner is inputting the morse, e.g. '...', and translating it as three e's not a single s. I had tried excepting cases (if(!.contains)) (more or less) but it was not working and seemed excessive conditions. I feel I am just not treating the correct objects in the correct manner. Really, I'd be fine if the second case (two morse characters, separated by a space) would function. A single morse character might be too much to ask at this point. The grand question: where am I telling Java it's ok to treat three dots as three separate letters? And why?...... I hope that is helpful.