doocs/leetcode · error · IllegalArgumentException
Invalid char:
Error message
Invalid char:
What it means
This is the Java Zuma Game encode() switch inside the Chinese README for LeetCode 488. Characters are mapped R->0x1, G->0x2, B->0x3, W->0x4, Y->0x5, space->0x0; the default branch throws IllegalArgumentException naming the offending character. It documents that any input outside [RGBYW ] is a programming error, not a runtime condition to recover from.
Source
Thrown at solution/0400-0499/0488.Zuma Game/README.md:301
}
long stateBits = 0;
for (char ch : stateChars) {
stateBits = (stateBits << 3) | Zuma.encode(ch);
}
return stateBits;
}
private static long encode(char ch) {
return switch (ch) {
case 'R' -> 0x1;
case 'G' -> 0x2;
case 'B' -> 0x3;
case 'W' -> 0x4;
case 'Y' -> 0x5;
case ' ' -> 0x0;
default ->
throw new IllegalArgumentException("Invalid char: " + ch);
};
}
}
```
<!-- tabs:end -->
<!-- solution:end -->
<!-- problem:end -->
View on GitHub (pinned to f84f361dc4)
Solutions
- Normalize input first: s = s.trim().toUpperCase() and filter to [RGBYW]
- Extend the switch when adding new colors to the game
- Validate with a character whitelist loop or regex before encoding
- Check the exception message — it prints the exact char that failed
Example fix
// before
encode("RgB"); // throws Invalid char: g
// after
encode("RgB".toUpperCase()); // ok Defensive patterns
Strategy: validation
Validate before calling
String s2 = s.trim().toUpperCase();
if (!s2.matches("[RGBYW ]*")) throw new IllegalArgumentException("Invalid char in: " + s2); Type guard
private static boolean isValidBoardChar(char c) { return "RGBYW ".indexOf(c) >= 0; } Try / catch
try { encode(input); } catch (IllegalArgumentException e) { /* message names the bad char; sanitize input and retry */ } Prevention
- Uppercase and trim tutorial inputs
- Avoid copy-paste of invisible characters
- Keep the switch in sync with new colors
When it happens
Trigger: Compiling the README snippet and calling encode with lowercase letters, punctuation, or whitespace other than plain space (tabs, CR/LF from file reads).
Common situations: Following the tutorial with hand-typed input that includes a typo or wrong case; reading boards from files without trim(); extending the solution to more colors but forgetting the encoder; mixed full-width characters from CJK input methods.
Related errors
AI-assisted analysis of doocs/leetcode@f84f361dc4 (2026-08-27).
Data as JSON: /api/errors/e8ff76e35e2bbff7.
Report an issue: GitHub.