英文:
turning a null string into a white space in a morse code reader
问题
import java.util.Arrays;
public class MorseCodeDecoder {
public static String decode(String morseCode) {
String[] split = morseCode.trim().split(" ");
int length = split.length;
System.out.println(length);
System.out.println("this is split = " + Arrays.toString(split));
MorseCode morseCoder = new MorseCode();
String message = "";
int nullCount = 0; // Move this outside the loop
for (int i = 0; i < length; i++) {
// Check for null Morse code
if (morseCoder.get(split[i]) == null) {
nullCount++;
System.out.println(nullCount);
if (nullCount == 2) {
nullCount = 0;
message = message + " ";
}
} else {
nullCount = 0; // Reset nullCount if valid Morse code is found
System.out.println(morseCoder.get(split[i]));
message = message + morseCoder.get(split[i]);
}
System.out.println(message);
}
return message;
}
}
Remember to replace the existing MorseCodeDecoder
class in your code with this updated version. This version of the code should handle the null detection and space detection correctly, and it should produce the desired output: "HEY JUDE".
英文:
I am trying to write a morse code translator for a challenge. at the moment I have got as far as translating the morse code into a String array and am passing it through a for
loop to build the message.
I am currently getting HEYnullnullJUDE
and am working out how to detect 2 spaces which in morse code will act as a word space. however when I use an if
statement to detect .isEmpty
or .isBlank
or .length() == 0
the for
loop exits and I get a return of:
9
this is split = [...., ., -.--, , , .---, ..-, -.., .]
H
H
E
HE
Y
HEY
for some reason once it hits Null
, instead of executing the nullCount
it just exits and return HEY
and I'm not sure.
what is the best way of turning HEYnullnullJUDE
which i am getting now into HEY JUDE
and detecting the double space in the morse code?
this is taken from this codewars challenge: https://www.codewars.com/kata/54b724efac3d5402db00065e/train/java
import java.util.Arrays;
public class MorseCodeDecoder {
public static String decode(String morseCode) {
String[] split = morseCode.trim().split(" ");
int length = split.length;
System.out.println(length);
System.out.println("this is split = " + Arrays.toString(split));
MorseCode morseCoder = new MorseCode();
String message = "";
for (int i =0; i < length; i++){
int nullCount = 0;
// this is where the code exits for some reason
if(morseCoder.get(split[i]) == null{
nullCount++;
System.out.println(nullCount);
if (nullCount == 2){
nullCount =0;
message = message + " ";
}
}
// if this is "if"statment is removed i get HEYnullnullJUDE
System.out.println (morseCoder.get(split[i]));
message = message + morseCoder.get(split[i]);
System.out.println(message);
}
return message;
}
}
samble test
import org.junit.Test;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
public class MorseCodeDecoderTest {
@Test
public void testExampleFromDescription() {
assertThat(MorseCodeDecoder.decode(".... . -.-- .--- ..- -.. ."), is("HEY JUDE"));
}
}
答案1
得分: 1
你的输入是 ".... . -.-- .--- ..- -.. ."
。
由于你调用了 trim()
,所以在 split()
中不会有前导或尾随空格。
为了将结果中的 3 个空格视为单个空格,并且不在这 3 个空格之间进行拆分,你基本上不希望在这 3 个空格之间拆分。换句话说,你只想在非空格之后或者紧跟在非空格之后的空格处进行拆分。
在这些地方拆分
↓ ↓ ↓ ↓ ↓ ↓ ↓
.... . -.-- .--- ..- -.. .
↑
不要在这里拆分
如果你使用 split("(?<=[^ ]) | (?=[^ ])")
,那么结果将变为:
{ "....", ".", "-.--", " ", ".---", "..-", "-..", "." }
在这里,摩斯码中的空格真正变成了一个包含单个空格的字符串。
剩下的部分就交给你处理了,例如如何使 morseCoder.get(" ")
返回一个 " "
(或者 ' '
,具体类型可以根据情况而定)。
更新
根据评论:
> MorseCode.get
不包括对空格的检测
morseCoder.get(" ")
返回 " "
只是解决方案的一个示例。或者,可以在调用 morseCoder.get(...)
之前检查是否遇到了空格,例如:
StringBuilder buf = new StringBuilder();
for (String symbol : split) {
if (symbol.equals(" ")) {
buf.append(' ');
} else {
String str = morseCoder.get(split[i]);
buf.append(str == null ? "�" : str);
}
}
String message = buf.toString();
英文:
Your input is ".... . -.-- .--- ..- -.. ."
.
Since you call trim()
, there will be no leading or trailing spaces seen by split()
.
To see the 3 spaces as a single space in the result of the split()
, you basically don't want to split on the middle of the 3 spaces. Said another way, you only want to split on a space that follows a non-space or a space that is followed by a non-space.
Split on these
↓ ↓ ↓ ↓ ↓ ↓ ↓
.... . -.-- .--- ..- -.. .
↑
Don't split on this
If you use split("(?<=[^ ]) | (?=[^ ])")
, then the result becomes:
{ "....", ".", "-.--", " ", ".---", "..-", "-..", "." }
Here the space in the morse code truly becomes a string with a single space.
I'll leave the rest to you, e.g. how to make morseCoder.get(" ")
return a " "
(or ' '
, whatever the type may be).
UPDATE
From comment:
> MorseCode.get does not include " " space detection
morseCoder.get(" ")
return a " "
was just an example of how it could be solved. Alternatively, simply check if a space is encountered, before calling morseCoder.get(...)
, e.g.
StringBuilder buf = new StringBuider();
for (String symbol : split) {
if (symbol.equals(" ")) {
buf.append(' ');
} else {
String str = morseCoder.get(split[i]);
buf.append(str == null ? "�" : str);
}
}
String message = buf.toString();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论