chinabugotech/hutool · error · IllegalArgumentException
Incorrect morse.
Error message
Incorrect morse.
What it means
Thrown by Morse.decode() when the input string contains characters other than the configured dit, dah, and split characters. The method uses StrUtil.containsOnly() to verify the input is composed exclusively of these three characters before attempting decoding. The default dit is '.', dah is '-', and split is '/'.
Source
Thrown at hutool-core/src/main/java/cn/hutool/core/codec/Morse.java:154
morseBuilder.append(word.replace('0', dit).replace('1', dah)).append(split);
}
return morseBuilder.toString();
}
/**
* 解码
*
* @param morse 莫尔斯电码
* @return 明文
*/
public String decode(String morse) {
Assert.notNull(morse, "Morse should not be null.");
final char dit = this.dit;
final char dah = this.dah;
final char split = this.split;
if (false == StrUtil.containsOnly(morse, dit, dah, split)) {
throw new IllegalArgumentException("Incorrect morse.");
}
final List<String> words = StrUtil.split(morse, split);
final StringBuilder textBuilder = new StringBuilder();
Integer codePoint;
for (String word : words) {
if(StrUtil.isEmpty(word)){
continue;
}
word = word.replace(dit, '0').replace(dah, '1');
codePoint = DICTIONARIES.get(word);
if (codePoint == null) {
codePoint = Integer.valueOf(word, 2);
}
textBuilder.appendCodePoint(codePoint);
}
return textBuilder.toString();
}
}View on GitHub (pinned to 8870454b2a)
Solutions
- Ensure you are passing morse-encoded input to decode(), not plain text.
- If using a custom Morse(char dit, char dah, char split) instance, make sure the input uses those exact characters.
- Validate or sanitize input before calling decode — strip or reject unexpected characters.
- Use encode() to produce morse code first if you need to test round-tripping.
Example fix
// before
Morse morse = new Morse();
String text = morse.decode("hello world"); // throws — plain text, not morse
// after
String encoded = morse.encode("HELLO WORLD"); // produces morse like "......-..-..---/.-- --- .-. .-.. -.."
String text = morse.decode(encoded); // round-trips correctly Defensive patterns
Strategy: validation
Validate before calling
// Verify input contains only dit, dah, and split characters Morse morse = new Morse(); boolean valid = StrUtil.containsOnly(input, '.', '-', '/'); // or for custom Morse instances: // boolean valid = StrUtil.containsOnly(input, ditChar, dahChar, splitChar);
Try / catch
try {
String text = morse.decode(morseInput);
} catch (IllegalArgumentException e) {
// input contains invalid characters
throw new IllegalArgumentException("Input is not valid morse code", e);
} Prevention
- Always use encode() output as input to decode() for round-trip testing.
- Validate input character set before calling decode().
- Document the expected dit/dah/split characters for consumers of your API.
When it happens
Trigger: Calling morse.decode(input) with a string containing any character that is not the dit ('.'), dah ('-'), or split ('/') character (or whichever custom characters were configured via the Morse(char, char, char) constructor). For example, passing plain text, uppercase letters, or spaces.
Common situations: Passing plain text to decode() instead of morse code. Mixing up encode() and decode() calls. Using a custom Morse instance with non-default characters but passing input formatted with default characters. Confusion between dit/dash representations (e.g., using '*' instead of '.' for dots).
Related errors
- invalid hash: {hash}
- Invalid alphabet for hash
- BAD_INPUT
- Illegal hexadecimal character {} at index {}
- Base58 checksum is invalid
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/830b853148eeae1f.
Report an issue: GitHub.