Skip to content

fix: handle Null Dereference in RomanToInteger #5461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .inferconfig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"report-block-list-path-regex": [
"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/PNCounter.java",
"src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ private 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

/**
Expand All @@ -39,10 +43,10 @@ public static int romanToInt(String a) {

if (prev != ' ') {
// 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 (currentNum >= newPrev) {
Expand All @@ -57,9 +61,4 @@ public static int romanToInt(String a) {

return sum;
}

public static void main(String[] args) {
int sum = romanToInt("MDCCCIV");
System.out.println(sum);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.thealgorithms.conversions;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

Expand All @@ -10,5 +11,13 @@ public class RomanToIntegerTest {
public void testRomanToInteger() {
assertEquals(1994, RomanToInteger.romanToInt("MCMXCIV"));
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"));
}
}