{"record":{"id":"830b853148eeae1f","repo":"chinabugotech/hutool","slug":"incorrect-morse","errorCode":null,"errorMessage":"Incorrect morse.","messagePattern":"Incorrect morse\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/codec/Morse.java","lineNumber":154,"sourceCode":"\t\t\tmorseBuilder.append(word.replace('0', dit).replace('1', dah)).append(split);\n\t\t}\n\t\treturn morseBuilder.toString();\n\t}\n\n\t/**\n\t * 解码\n\t *\n\t * @param morse 莫尔斯电码\n\t * @return 明文\n\t */\n\tpublic String decode(String morse) {\n\t\tAssert.notNull(morse, \"Morse should not be null.\");\n\n\t\tfinal char dit = this.dit;\n\t\tfinal char dah = this.dah;\n\t\tfinal char split = this.split;\n\t\tif (false == StrUtil.containsOnly(morse, dit, dah, split)) {\n\t\t\tthrow new IllegalArgumentException(\"Incorrect morse.\");\n\t\t}\n\t\tfinal List<String> words = StrUtil.split(morse, split);\n\t\tfinal StringBuilder textBuilder = new StringBuilder();\n\t\tInteger codePoint;\n\t\tfor (String word : words) {\n\t\t\tif(StrUtil.isEmpty(word)){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tword = word.replace(dit, '0').replace(dah, '1');\n\t\t\tcodePoint = DICTIONARIES.get(word);\n\t\t\tif (codePoint == null) {\n\t\t\t\tcodePoint = Integer.valueOf(word, 2);\n\t\t\t}\n\t\t\ttextBuilder.appendCodePoint(codePoint);\n\t\t}\n\t\treturn textBuilder.toString();\n\t}\n}","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/codec/Morse.java#L136-L172","documentation":"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 '/'.","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"// before\nMorse morse = new Morse();\nString text = morse.decode(\"hello world\"); // throws — plain text, not morse\n\n// after\nString encoded = morse.encode(\"HELLO WORLD\"); // produces morse like \"......-..-..---/.-- --- .-. .-.. -..\"\nString text = morse.decode(encoded); // round-trips correctly","handlingStrategy":"validation","validationCode":"// Verify input contains only dit, dah, and split characters\nMorse morse = new Morse();\nboolean valid = StrUtil.containsOnly(input, '.', '-', '/');\n// or for custom Morse instances:\n// boolean valid = StrUtil.containsOnly(input, ditChar, dahChar, splitChar);","typeGuard":null,"tryCatchPattern":"try {\n    String text = morse.decode(morseInput);\n} catch (IllegalArgumentException e) {\n    // input contains invalid characters\n    throw new IllegalArgumentException(\"Input is not valid morse code\", e);\n}","preventionTips":["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."],"tags":["morse","decode","input-validation","codec"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}