Convert a Roman Number to Decimal using Hashmap in Java

Given a Roman numeral, the task is to find the corresponding decimal value.

Note: Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M.

Examples:

Input: “III”

Output: 3



Input: “MDCCLX”

Output: 1760

Approach:

  1. Loop through each character in the string containing the Roman numerals.
  2. Compare the value of the current roman symbol with the value of the roman symbol to its right. If the current value is greater than or equal to the value of the symbol to the right, add the current symbol’s value to the total. If the current value is smaller than the value of the symbol to the right, subtract the current symbol’s value from the total.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Convert a Roman
// Number to Decimal using Hashmap
import java.io.*;
import java.util.Scanner;
import java.util.HashMap;
class solution {
    int romanToInt(String s)
    {
        // Create a empty hash map.
        HashMap<Character, Integer> map = new HashMap<>();
  
        // Putting value in hash map.
        map.put('I', 1);
        map.put('V', 5);
        map.put('X', 10);
        map.put('L', 50);
        map.put('C', 100);
        map.put('D', 500);
        map.put('M', 1000);
  
        // Creating integer variable to store result.
        int result = 0;
  
        // initialize loop to iterate in string.
        for (int i = 0; i < s.length(); i++) {
  
            // Checking that current element
            // is not smaller then previous
            if (i > 0
                && map.get(s.charAt(i))
                       > map.get(s.charAt(i - 1))) {
                result += map.get(s.charAt(i))
                          - 2 * map.get(s.charAt(i - 1));
            }
            else {
                result += map.get(s.charAt(i));
            }
        }
        // Returning the integer value of Roman number.
        return result;
    }
}
public class GFG {
  
    public static void main(String[] args)
    {
        String s;
        
        // Scanner sc = new Scanner(System.in);
        // s = sc.nextLine();
        solution gfg = new solution();
        System.out.println(gfg.romanToInt("MDCCLX"));
    }
}

chevron_right


Output:

1760

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.