Repository: TheAlgorithms/Java
Pull request #5461 (2024-09-23 07:19:47)

fix: handle Null Dereference in `RomanToInteger`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

Continuation of #5456.

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5461?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 52.19%. Comparing base [(`b849cbb`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b849cbb60210b43c18195cc713bed8b44cc8610c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`dc5842e`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/dc5842eb547819104a84a807683cff5fec1105e0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5461      +/-   ##
============================================
+ Coverage     52.17%   52.19%   +0.01%     
- Complexity     3245     3247       +2     
============================================
  Files           524      524              
  Lines         15158    15159       +1     
  Branches       2881     2881              
============================================
+ Hits           7909     7912       +3     
+ Misses         6924     6923       -1     
+ Partials        325      324       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5461?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/.inferconfig b/.inferconfig
index 93bd1c35..e7af307a 100644
--- a/.inferconfig
+++ b/.inferconfig
@@ -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",
diff --git a/src/main/java/com/thealgorithms/conversions/RomanToInteger.java b/src/main/java/com/thealgorithms/conversions/RomanToInteger.java
index cf2d4145..1934e9b2 100644
--- a/src/main/java/com/thealgorithms/conversions/RomanToInteger.java
+++ b/src/main/java/com/thealgorithms/conversions/RomanToInteger.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
 
     /**
@@ -39,10 +43,10 @@ public final class RomanToInteger {
 
             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) {
@@ -57,9 +61,4 @@ public final class RomanToInteger {
 
         return sum;
     }
-
-    public static void main(String[] args) {
-        int sum = romanToInt("MDCCCIV");
-        System.out.println(sum);
-    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/RomanToIntegerTest.java b/src/test/java/com/thealgorithms/conversions/RomanToIntegerTest.java
index c51986f2..f0356397 100644
--- a/src/test/java/com/thealgorithms/conversions/RomanToIntegerTest.java
+++ b/src/test/java/com/thealgorithms/conversions/RomanToIntegerTest.java
@@ -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;
 
@@ -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"));
     }
 }