TheAlgorithms/Java · error · IllegalArgumentException
inputUnit must be different from outputUnit.
Error message
inputUnit must be different from outputUnit.
What it means
Thrown by UnitsConverter.convert when inputUnit.equals(outputUnit) — the library refuses no-op conversions and requires distinct units. This check fires before any lookup, so even units not in the converter set will trip this guard if both strings are equal.
Solutions
- Short-circuit in the caller: if inputUnit.equals(outputUnit), return the value unchanged instead of calling convert.
- Validate that the two unit parameters are distinct before invoking convert.
- If the same-unit case is valid in your domain, handle it explicitly before the call.
Example fix
// before
return converter.convert(unit, unit, value);
// after
if (inputUnit.equals(outputUnit)) {
return value;
}
return converter.convert(inputUnit, outputUnit, value); Defensive patterns
Strategy: validation
Validate before calling
static double safeConvert(UnitsConverter c, String in, String out, double v) {
if (in == null || out == null) throw new IllegalArgumentException("units must not be null");
if (in.equals(out)) return v; // no-op short-circuit
return c.convert(in, out, v);
} Type guard
static boolean isDistinctNonNull(String a, String b) {
return a != null && b != null && !a.equals(b);
} Try / catch
try {
return converter.convert(inputUnit, outputUnit, value);
} catch (IllegalArgumentException e) {
if (inputUnit.equals(outputUnit)) return value; // legitimate no-op
throw e;
} Prevention
- Short-circuit same-unit conversions in the caller before invoking convert.
- When source and target units come from independent selectors, validate distinctness on selection change.
- Document that same-unit calls are rejected by design.
When it happens
Trigger: Calling converter.convert("Celsius", "Celsius", 100), or passing the same variable for both arguments (e.g., convert(unit, unit, val)).
Common situations: Building generic pipelines where source and target unit come from the same config field or are user-selected independently; refactoring that accidentally passes the same variable twice.
Related errors
- Invalid unit ' '. Supported units are
- Base must be greater than 01
- Base must be greater than 02
- Buffer size must be positive
- Cannot delete from an empty tree
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/83eba6b71811f35b.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/com/thealgorithms/conversions/UnitsConverter.java:133
*/
public UnitsConverter(final Map<Pair<String, String>, AffineConverter> basicConversions) {
conversions = computeAllConversions(basicConversions);
units = extractUnits(conversions);
}
/**
* Converts a value from one unit to another.
*
* @param inputUnit the unit of the input value.
* @param outputUnit the unit to convert the value into.
* @param value the value to convert.
* @return the converted value in the target unit.
* @throws IllegalArgumentException if inputUnit equals outputUnit.
* @throws NoSuchElementException if no conversion exists between the units.
*/
public double convert(final String inputUnit, final String outputUnit, final double value) {
if (inputUnit.equals(outputUnit)) {
throw new IllegalArgumentException("inputUnit must be different from outputUnit.");
}
final var conversionKey = Pair.of(inputUnit, outputUnit);
return conversions.computeIfAbsent(conversionKey, k -> { throw new NoSuchElementException("No converter for: " + k); }).convert(value);
}
/**
* Retrieves the set of all units supported by this converter.
*
* @return a set of available units.
*/
public Set<String> availableUnits() {
return units;
}
}
View on GitHub (pinned to fdfb9a395b)