{"record":{"id":"c6ab926c17f972fe","repo":"doocs/leetcode","slug":"invalid-char-c6ab92","errorCode":null,"errorMessage":"Invalid char: ","messagePattern":"Invalid char: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"solution/0400-0499/0488.Zuma Game/README_EN.md","lineNumber":287,"sourceCode":"        }\n\n        long stateBits = 0;\n        for (char ch : stateChars) {\n            stateBits = (stateBits << 3) | Zuma.encode(ch);\n        }\n        return stateBits;\n    }\n\n    private static long encode(char ch) {\n        return switch (ch) {\n            case 'R' -> 0x1;\n            case 'G' -> 0x2;\n            case 'B' -> 0x3;\n            case 'W' -> 0x4;\n            case 'Y' -> 0x5;\n            case ' ' -> 0x0;\n            default  ->\n                throw new IllegalArgumentException(\"Invalid char: \" + ch);\n        };\n    }\n}\n```\n\n<!-- tabs:end -->\n\n<!-- solution:end -->\n\n<!-- problem:end -->\n","sourceCodeStart":269,"sourceCodeEnd":298,"githubUrl":"https://github.com/doocs/leetcode/blob/f84f361dc403a65c4c031f6e0774297cc377f00a/solution/0400-0499/0488.Zuma Game/README_EN.md#L269-L298","documentation":"English README version of the LeetCode 488 Java encoder: the switch maps only R, G, B, W, Y, and space; anything else triggers IllegalArgumentException(\"Invalid char: \" + ch) in the default branch. The exception exists to surface malformed board/hand strings immediately instead of producing corrupted encoded state.","triggerScenarios":"Passing strings containing digits, lowercase color letters, '\\n', '\\t', or unicode chars to encode(); consuming input from stdin without stripping the newline.","commonSituations":"Online-judge style runners appending a newline to the last line; users substituting different color letters for variant puzzles; refactoring from char[] to String and picking up unexpected separators.","solutions":["Strip and validate: input.chars().allMatch(c -> \"RGBYW \".indexOf(c) >= 0)","Use trim()/strip() on lines read from input streams","Add missing cases to the switch if your variant defines extra colors","Log the exact input string when the exception fires to find the bad byte"],"exampleFix":"// before\nencode(boardLine); // boardLine ends with '\\n' -> throws\n\n// after\nencode(boardLine.strip());","handlingStrategy":"validation","validationCode":"String line = scanner.nextLine().strip();\nif (!line.matches(\"[RGBYW ]*\")) { System.err.println(\"bad input: \" + line); continue; }\nencode(line);","typeGuard":"static boolean isEncodable(String s) { return s.chars().allMatch(c -> \"RGBYW \".indexOf(c) >= 0); }","tryCatchPattern":"try { encode(s); } catch (IllegalArgumentException e) { if (e.getMessage().startsWith(\"Invalid char:\")) fixInput(); else throw e; }","preventionTips":["Strip newlines from stdin lines","Pre-validate with a whitelist","Log raw input on failure"],"tags":["java","validation","illegal-argument","encoding","documentation"],"backgroundTag":"invalid-input-character","analyzedSha":"f84f361dc403a65c4c031f6e0774297cc377f00a","analyzedAt":"2026-08-27T04:29:31.522Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}