TheAlgorithms/Java · error · IllegalArgumentException

Unknown Roman symbol: {}

Error message

Unknown Roman symbol: {}

What it means

Thrown by RomanToInteger.romanSymbolToInt (called internally by romanToInt) when a character in the input string is not one of the seven valid Roman numeral symbols: I, V, X, L, C, D, M. The method uppercases the input first, so lowercase variants are accepted, but any other character triggers this error. The offending character is included in the error message.

Source

Thrown at src/main/java/com/thealgorithms/conversions/RomanToInteger.java:51

            put('L', 50);
            put('C', 100);
            put('D', 500);
            put('M', 1000);
        }
    };

    private RomanToInteger() {
    }

    /**
     * Converts a single Roman numeral character to its integer value.
     *
     * @param symbol the Roman numeral character
     * @return the corresponding integer value
     * @throws IllegalArgumentException if the symbol is not a valid Roman numeral
     */
    private static int romanSymbolToInt(final char symbol) {
        return ROMAN_TO_INT.computeIfAbsent(symbol, c -> { throw new IllegalArgumentException("Unknown Roman symbol: " + c); });
    }

    /**
     * Converts a Roman numeral string to its integer equivalent.
     * Steps:
     * <ol>
     *     <li>Iterate over the string from right to left.</li>
     *     <li>For each character, convert it to an integer value.</li>
     *     <li>If the current value is greater than or equal to the max previous value, add it.</li>
     *     <li>Otherwise, subtract it from the sum.</li>
     *     <li>Update the max previous value.</li>
     *     <li>Return the sum.</li>
     * </ol>
     *
     * @param roman the Roman numeral string
     * @return the integer value of the Roman numeral
     * @throws IllegalArgumentException if the input contains invalid Roman characters
     * @throws NullPointerException if the input is {@code null}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Trim whitespace and validate the input with a regex like ^[IVXLCDMivxlcdm]+$ before calling romanToInt.
  2. If the input may contain prefixes or suffixes, strip them before conversion.
  3. Provide a clear input format specification at the UI or API boundary.

Example fix

// before
int result = RomanToInteger.romanToInt(romanStr);

// after
romanStr = romanStr.trim();
if (!romanStr.matches("^[IVXLCDMivxlcdm]+$")) {
    throw new IllegalArgumentException("Input must contain only Roman numeral characters I, V, X, L, C, D, M: " + romanStr);
}
int result = RomanToInteger.romanToInt(romanStr);
Defensive patterns

Strategy: validation

Validate before calling

roman = roman.trim();
if (!roman.matches("^[IVXLCDMivxlcdm]+$")) {
    throw new IllegalArgumentException("Input must contain only Roman numeral characters: " + roman);
}
int result = RomanToInteger.romanToInt(roman);

Type guard

static boolean isValidRoman(String s) {
    return s != null && !s.isEmpty() && s.trim().matches("^[IVXLCDMivxlcdm]+$");
}

Try / catch

try {
    int result = RomanToInteger.romanToInt(roman);
} catch (IllegalArgumentException e) {
    // invalid character present; log and skip or sanitize
    logger.warn("Invalid Roman numeral: {}", roman);
}

Prevention

When it happens

Trigger: Passing strings containing spaces, digits, punctuation, or letters outside the Roman set (e.g., 'XII A', '1V', 'MCM@XC'). Providing Arabic numerals instead of Roman. Including leading/trailing whitespace that is not trimmed.

Common situations: User input that was not validated before conversion. Data from a source that mixes Roman numerals with other text. Whitespace or formatting characters embedded in the string from a file or database.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/6a49584dff3c695d. Report an issue: GitHub.