mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
fix: handle Null Dereference in RomanToInteger
(#5461)
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
{
|
{
|
||||||
"report-block-list-path-regex": [
|
"report-block-list-path-regex": [
|
||||||
"src/main/java/com/thealgorithms/ciphers/a5/CompositeLFSR.java",
|
"src/main/java/com/thealgorithms/ciphers/a5/CompositeLFSR.java",
|
||||||
"src/main/java/com/thealgorithms/conversions/RomanToInteger.java",
|
|
||||||
"src/main/java/com/thealgorithms/datastructures/crdt/GCounter.java",
|
"src/main/java/com/thealgorithms/datastructures/crdt/GCounter.java",
|
||||||
"src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java",
|
"src/main/java/com/thealgorithms/datastructures/crdt/PNCounter.java",
|
||||||
"src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java",
|
"src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java",
|
||||||
|
@ -19,6 +19,10 @@ public final class RomanToInteger {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static int romanSymbolToInt(final char symbol) {
|
||||||
|
return ROMAN_TO_INT.computeIfAbsent(symbol, c -> { throw new IllegalArgumentException("Unknown Roman symbol: " + c); });
|
||||||
|
}
|
||||||
|
|
||||||
// Roman Number = Roman Numerals
|
// Roman Number = Roman Numerals
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,10 +43,10 @@ public final class RomanToInteger {
|
|||||||
|
|
||||||
if (prev != ' ') {
|
if (prev != ' ') {
|
||||||
// checking current Number greater than previous or not
|
// checking current Number greater than previous or not
|
||||||
newPrev = ROMAN_TO_INT.get(prev) > newPrev ? ROMAN_TO_INT.get(prev) : newPrev;
|
newPrev = romanSymbolToInt(prev) > newPrev ? romanSymbolToInt(prev) : newPrev;
|
||||||
}
|
}
|
||||||
|
|
||||||
int currentNum = ROMAN_TO_INT.get(c);
|
int currentNum = romanSymbolToInt(c);
|
||||||
|
|
||||||
// if current number greater than prev max previous then add
|
// if current number greater than prev max previous then add
|
||||||
if (currentNum >= newPrev) {
|
if (currentNum >= newPrev) {
|
||||||
@ -57,9 +61,4 @@ public final class RomanToInteger {
|
|||||||
|
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
int sum = romanToInt("MDCCCIV");
|
|
||||||
System.out.println(sum);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.thealgorithms.conversions;
|
package com.thealgorithms.conversions;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@ -10,5 +11,13 @@ public class RomanToIntegerTest {
|
|||||||
public void testRomanToInteger() {
|
public void testRomanToInteger() {
|
||||||
assertEquals(1994, RomanToInteger.romanToInt("MCMXCIV"));
|
assertEquals(1994, RomanToInteger.romanToInt("MCMXCIV"));
|
||||||
assertEquals(58, RomanToInteger.romanToInt("LVIII"));
|
assertEquals(58, RomanToInteger.romanToInt("LVIII"));
|
||||||
|
assertEquals(1804, RomanToInteger.romanToInt("MDCCCIV"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRomanToIntegerThrows() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> RomanToInteger.romanToInt("Z"));
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> RomanToInteger.romanToInt("MZI"));
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> RomanToInteger.romanToInt("MMMO"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user