{"record":{"id":"bca2f8ee1f226b7c","repo":"ben-manes/caffeine","slug":"key-s-value-was-set-to-s-must-be-an-integer","errorCode":null,"errorMessage":"key %s value was set to %s, must be an integer","messagePattern":"key (.+?) value was set to (.+?), must be an integer","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java","lineNumber":279,"sourceCode":"    requireArgument(refreshAfterWrite == null, \"refreshAfterWrite was already set\");\n    refreshAfterWrite = parseDuration(key, value);\n  }\n\n  /** Configures the value as weak or soft references. */\n  void recordStats(@Nullable String value) {\n    requireArgument(value == null, \"record stats does not take a value\");\n    requireArgument(!recordStats, \"record stats was already set\");\n    recordStats = true;\n  }\n\n  /** Returns a parsed int value. */\n  static int parseInt(String key, @Nullable String value) {\n    requireArgument((value != null) && !value.isEmpty(), \"value of key %s was omitted\", key);\n    requireNonNull(value);\n    try {\n      return Integer.parseInt(normalizeNumericLiteral(value));\n    } catch (NumberFormatException e) {\n      throw new IllegalArgumentException(String.format(US,\n          \"key %s value was set to %s, must be an integer\", key, value), e);\n    }\n  }\n\n  /** Returns a parsed long value. */\n  static long parseLong(String key, @Nullable String value) {\n    requireArgument((value != null) && !value.isEmpty(), \"value of key %s was omitted\", key);\n    requireNonNull(value);\n    try {\n      return Long.parseLong(normalizeNumericLiteral(value));\n    } catch (NumberFormatException e) {\n      throw new IllegalArgumentException(String.format(US,\n          \"key %s value was set to %s, must be a long\", key, value), e);\n    }\n  }\n\n  /** Returns the value after adjusting for underscores in a numeric literal. */\n  static String normalizeNumericLiteral(String value) {","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/ben-manes/caffeine/blob/9da6581ee366aa63c51e0dc96692d02f9c29ccff/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java#L261-L297","documentation":"CaffeineSpec.parseInt wraps Integer.parseInt and throws IllegalArgumentException(\"key %s value was set to %s, must be an integer\") when an option that requires an int (e.g. initialCapacity) receives a non-integer value. The message names both the offending key and the raw value so the bad setting can be located directly in the spec string.","triggerScenarios":"Caffeine.from(\"initialCapacity=1_0x\"), values with commas (\"maximumSize=1,000\"), decimal values (\"initialCapacity=64.5\"), or embedded units (\"initialCapacity=64k\") on an int option.","commonSituations":"Human-formatted numbers (thousands separators) pasted from docs into config; locale-formatted values; environment variable interpolation inserting stray characters; underscores misplaced (leading/trailing, after sign) which normalizeNumericLiteral leaves as-is.","solutions":["Use a plain decimal integer for the key shown in the message, e.g. maximumSize=1000 (commas are not valid)","Underscores as thousands separators are allowed only between digits: 1_000_000 is fine, _1000 and 1_000_ are not","Strip formatting before injecting values into the spec string"],"exampleFix":"# before\nCaffeine.from(\"initialCapacity=1,024\") // IllegalArgumentException: must be an integer\n\n# after\nCaffeine.from(\"initialCapacity=1024\")","handlingStrategy":"validation","validationCode":"// Validate int options before building the spec:\nstatic String intOption(String key, String rawValue) {\n  String normalized = rawValue.replace(\"_\", \"\").replace(\",\", \"\").strip();\n  if (!normalized.matches(\"[+-]?\\\\d+\")) {\n    throw new ConfigException(key + \" must be an integer, got: \" + rawValue);\n  }\n  return key + \"=\" + normalized;\n}\nString spec = intOption(\"initialCapacity\", envValue);","typeGuard":null,"tryCatchPattern":"try {\n  return Caffeine.from(spec);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"must be an integer\")) {\n    log.error(\"Non-integer value in cache spec: {}\", spec, e);\n    return Caffeine.newBuilder(); // fall back to defaults\n  }\n  throw e;\n}","preventionTips":["Strip commas and whitespace from numeric config values before composing specs","Use underscores only between digits (1_000 valid; _1000, 1_000_, +_1 invalid)","Prefer programmatic configuration (Caffeine.newBuilder().initialCapacity(n)) over string specs when values come from untrusted sources"],"tags":["caffeine","configuration","caffeine-spec","number-parsing","validation"],"backgroundTag":null,"analyzedSha":"9da6581ee366aa63c51e0dc96692d02f9c29ccff","analyzedAt":"2026-08-14T14:45:28.147Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}