Java Convert Roman to Integer Number

In this source code example, we will write a Java program to convert Roman number to an integer number.
Check out Java 8 examples at Java 8 Examples
Checkout all Java programs at 100+ Java Programs 

Java Convert Roman to Integer Number

Here is a Java program to convert Roman number to an integer number:
package net.sourcecodeexamples.java.Conversions;

import java.util.*;

public class RomanToInteger {

    private static Map < Character, Integer > map = new HashMap < Character, Integer > () {
        private static final long serialVersionUID = 87605733047260530 L;

        {
            put('I', 1);
            put('V', 5);
            put('X', 10);
            put('L', 50);
            put('C', 100);
            put('D', 500);
            put('M', 1000);
        }
    };

    /**
     * This function convert Roman number into Integer
     *
     * @param A Roman number string
     * @return integer
     */
    public static int romanToInt(String A) {

        char prev = ' ';

        int sum = 0;

        int newPrev = 0;
        for (int i = A.length() - 1; i >= 0; i--) {
            char c = A.charAt(i);

            if (prev != ' ') {
                // checking current Number greater then previous or not
                newPrev = map.get(prev) > newPrev ? map.get(prev) : newPrev;
            }

            int currentNum = map.get(c);

            // if current number greater then prev max previous then add
            if (currentNum >= newPrev) {
                sum += currentNum;
            } else {
                // subtract upcoming number until upcoming number not greater then prev max
                sum -= currentNum;
            }
            prev = c;
        }

        return sum;
    }

    public static void main(String[] args) {
        try (Scanner input = new Scanner(System.in)) {
            System.out.print("Enter the Roman number : ");
            String number = input.next();
            int sum = romanToInt(number);
            System.out.print("Integer number : ");
            System.out.println(sum);
        }
    }
}

Output

Enter the Roman number : DCCVII
Integer number : 707
Check out Java 8 examples at Java 8 Examples
Checkout all Java programs at 100+ Java Programs 



Comments