Java 8 – How to convert Roman letter/numbers to Integer ?

In this article, we will learn how to convert Roman letters (I, II, III, IV, V, VI, etc.,) into Integer (1, 2, 3, 4, 5, 6, etc.,) using Java 8 Stream

Convert Roman letters to Integer :

  1. Using Java 8 Stream
  2. Using traditional forloop and ifelse construct

1. Using Java 8 Stream :

  • Get the range of number starting from 0 to String‘s length using IntStream.range() method and then iterate using Stream.forEach() method
  • Use ifelse construct inside Stream.forEach() method to convert Roman letters to Integer as shown in the below illustration
  • Note: variables inside Stream‘s method should be final or effectively final otherwise it throws compiletime error stating “Local variable <variable-name> defined in an enclosing scope must be final or effectively final

Java8ConvertRomanLettersToInteger.java

package in.bench.resources.arrays.find.number;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.IntStream;

public class Java8ConvertRomanLettersToInteger {

	// main() method
	public static void main(String[] args) {

		// test Roman letters
		System.out.println("Integer/Number for Roman letter IV :- " 
				+ getIntegerForRomanLetter("IV"));
		System.out.println("Integer/Number for Roman letter XVII :- " 
				+ getIntegerForRomanLetter("XVII"));
		System.out.println("Integer/Number for Roman letter LIX :- "
				+ getIntegerForRomanLetter("LIX"));
		System.out.println("Integer/Number for Roman letter CLVIII :- "
				+ getIntegerForRomanLetter("CLVIII"));
		System.out.println("Integer/Number for Roman letter DI :- "
				+ getIntegerForRomanLetter("DI"));
		System.out.println("Integer/Number for Roman letter MDCCCCXXXXVII :- "
				+ getIntegerForRomanLetter("MDCCCCXXXXVII"));
		System.out.print("Integer/Number for Roman letter MMXXII :- " 
				+ getIntegerForRomanLetter("MMXXII"));
	}


	/**
	 * 1. This method converts Roman letter to Integer/Number
	 * 
	 * @param str
	 * @return
	 */
	private static int getIntegerForRomanLetter(String str) {

		// local variable
		final int[] number = {0};
		// https://karimsblog.medium.com/java-8-assigning-variable-inside-lambda-foreach-c73720f1860f


		// get Roman to Integer Map
		Map<Character, Integer>  map = romanToIntegerMap();


		// convert Roman to Integer using Stream
		IntStream.range(0, str.length())
		.forEach(i -> {
			if (i+1 == str.length() || map.get(str.charAt(i)) >= map.get(str.charAt(i + 1))) {
				number[0] += map.get(str.charAt(i));
			} else {
				number[0] -= map.get(str.charAt(i));
			}
		});


		// return converted integer/number
		return number[0];
	}


	/**
	 * 2. This method returns Roman to Integer conversion
	 * 
	 * @return
	 */
	private static Map<Character, Integer> romanToIntegerMap() {

		// create Map to store Roman letter to Integer
		Map<Character, Integer>  map = new LinkedHashMap<>();
		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);


		// return map
		return map;
	}
}

Output:

Integer/Number for Roman letter IV :- 4
Integer/Number for Roman letter XVII :- 17
Integer/Number for Roman letter LIX :- 59
Integer/Number for Roman letter CLVIII :- 158
Integer/Number for Roman letter DI :- 501
Integer/Number for Roman letter MDCCCCXXXXVII :- 1947
Integer/Number for Roman letter MMXXII :- 2022

2. Using traditional for-loop and if-else construct :

  • Iterate starting from 0 to String‘s length using traditional for-loop
  • Use ifelse construct to convert Roman letters to Integer as shown in the below illustration

ConvertRomanLettersToInteger.java

package in.bench.resources.arrays.find.number;

import java.util.LinkedHashMap;
import java.util.Map;

public class ConvertRomanLettersToInteger {

	// main() method
	public static void main(String[] args) {

		// test Roman letters
		System.out.println("Integer/Number for Roman letter IV :- " 
				+ getIntegerForRomanLetter("IV"));
		System.out.println("Integer/Number for Roman letter XVII :- " 
				+ getIntegerForRomanLetter("XVII"));
		System.out.println("Integer/Number for Roman letter LIX :- "
				+ getIntegerForRomanLetter("LIX"));
		System.out.println("Integer/Number for Roman letter CLVIII :- "
				+ getIntegerForRomanLetter("CLVIII"));
		System.out.println("Integer/Number for Roman letter DI :- "
				+ getIntegerForRomanLetter("DI"));
		System.out.println("Integer/Number for Roman letter MDCCCCXXXXVII :- "
				+ getIntegerForRomanLetter("MDCCCCXXXXVII"));
		System.out.print("Integer/Number for Roman letter MMXXII :- " 
				+ getIntegerForRomanLetter("MMXXII"));
	}


	/**
	 * 1. This method converts Roman letter to Integer/Number
	 * 
	 * @param str
	 * @return
	 */
	private static int getIntegerForRomanLetter(String str) {

		// local variable
		int number = 0;


		// get Roman to Integer Map
		Map<Character, Integer>  map = romanToIntegerMap();


		for (int i = 0; i < str.length(); i++) {
			if (i+1 == str.length() || map.get(str.charAt(i)) >= map.get(str.charAt(i + 1))) {
				number += map.get(str.charAt(i));
			} else {
				number -= map.get(str.charAt(i));
			}
		}


		// return converted integer/number
		return number;
	}


	/**
	 * 2. This method returns Roman to Integer conversion
	 * 
	 * @return
	 */
	private static Map<Character, Integer> romanToIntegerMap() {

		// create Map to store Roman letter to Integer
		Map<Character, Integer>  map = new LinkedHashMap<>();
		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);


		// return map
		return map;
	}
}

Output:

Integer/Number for Roman letter IV :- 4
Integer/Number for Roman letter XVII :- 17
Integer/Number for Roman letter LIX :- 59
Integer/Number for Roman letter CLVIII :- 158
Integer/Number for Roman letter DI :- 501
Integer/Number for Roman letter MDCCCCXXXXVII :- 1947
Integer/Number for Roman letter MMXXII :- 2022

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java 8 – How to convert Celsius to Fahrenheit and vice-versa ?
Java 8 – How to generate Fibonacci numbers using Stream ?