{"record":{"id":"1e80351da2d6bc31","repo":"chinabugotech/hutool","slug":"bad-number-at","errorCode":null,"errorMessage":"Bad number '{}{}' at: {}","messagePattern":"Bad number '(.+?)(.+?)' at: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java","lineNumber":527,"sourceCode":"\n\t\t// 节总和\n\t\tint section = 0;\n\t\tint number = 0;\n\t\tChineseUnit unit = null;\n\t\tchar c;\n\t\tfor (int i = 0; i < length; i++) {\n\t\t\tc = chinese.charAt(i);\n\t\t\tfinal int num = chineseToNumber(c);\n\t\t\tif (num >= 0) {\n\t\t\t\tif (num == 0) {\n\t\t\t\t\t// 遇到零时节结束，权位失效，比如两万二零一十\n\t\t\t\t\tif (number > 0 && null != unit) {\n\t\t\t\t\t\tsection += number * (unit.value / 10);\n\t\t\t\t\t}\n\t\t\t\t\tunit = null;\n\t\t\t\t} else if (number > 0) {\n\t\t\t\t\t// 多个数字同时出现，报错\n\t\t\t\t\tthrow new IllegalArgumentException(StrUtil.format(\"Bad number '{}{}' at: {}\", chinese.charAt(i - 1), c, i));\n\t\t\t\t}\n\t\t\t\t// 普通数字\n\t\t\t\tnumber = num;\n\t\t\t} else {\n\t\t\t\tunit = chineseToUnit(c);\n\t\t\t\tif (null == unit) {\n\t\t\t\t\t// 出现非法字符\n\t\t\t\t\tthrow new IllegalArgumentException(StrUtil.format(\"Unknown unit '{}' at: {}\", c, i));\n\t\t\t\t}\n\n\t\t\t\t//单位\n\t\t\t\tif (unit.secUnit) {\n\t\t\t\t\t// 节单位，按照节求和\n\t\t\t\t\tsection = (section + number) * unit.value;\n\t\t\t\t\tresult += section;\n\t\t\t\t\tsection = 0;\n\t\t\t\t} else {\n\t\t\t\t\t// 非节单位，和单位前的单数字组合为值","sourceCodeStart":509,"sourceCodeEnd":545,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java#L509-L545","documentation":"NumberChineseFormatter.chineseToNumber(String) parses Chinese numerals digit-by-digit. Each digit (零一二...九) must be followed by a unit (十百千万亿...). Two consecutive digits with no unit between them is illegal and throws IllegalArgumentException showing the offending pair and position.","triggerScenarios":"Calling chineseToNumber with input like \"一一\", \"二三\" (two digits back-to-back), or any string where a digit follows another digit without an intervening unit.","commonSituations":"User-supplied Chinese numeral input; data-entry mistakes; concatenation of partial numerals without units.","solutions":["Validate the input format before parsing: alternate digit and unit, allow a single trailing digit.","Sanitize/correct malformed input (e.g. insert 十 between consecutive digits) before calling.","Wrap the call in try/catch IllegalArgumentException and surface a user-facing error."],"exampleFix":"// before\nint n = NumberChineseFormatter.chineseToNumber(userInput);\n\n// after - validate then parse\nif (!userInput.matches(\"[零一二三四五六七八九][十百千万亿]*([零一二三四五六七八九][十百千万亿]*)*[零一二三四五六七八九]?\")) {\n    throw new IllegalArgumentException(\"Invalid chinese numeral: \" + userInput);\n}\nint n = NumberChineseFormatter.chineseToNumber(userInput);","handlingStrategy":"validation","validationCode":"// a digit must be followed by a unit (or be the last char)\nif (chinese.matches(\".*[零一二三四五六七八九][零一二三四五六七八九].*\")) {\n    throw new IllegalArgumentException(\"consecutive digits in chinese numeral: \" + chinese);\n}\nNumberChineseFormatter.chineseToNumber(chinese);","typeGuard":null,"tryCatchPattern":"try {\n    return NumberChineseFormatter.chineseToNumber(s);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Bad number\")) { /* show user-facing parse error */ }\n    else throw e;\n}","preventionTips":["Validate that consecutive Chinese digits are always separated by a unit.","Surface IllegalArgumentException as a user-facing validation error, not a 500."],"tags":["convert","chinese-numeral","parsing","validation"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}