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 :
- Using Java 8 Stream
- Using traditional for–loop and if–else 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 if–else 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 compile–time 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 if–else 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 :
- Java – To print first N natural numbers
- Java – Swapping two numbers without temporary variable
- Java – Swapping two numbers using third or temporary variable
- Java – Check whether the given number is Armstrong number or Not ?
- Java – Check whether number is Positive or Negative or Zero ?
- Java – Check whether number is Even or Odd ?
- Java – How to Reverse a Number in different ways ?
- Java – Check whether number is Prime or Not ?
- Java – Print Prime numbers between specified range or interval
- Java – How to find/remove first and last digit of a number ?
- Java – How to generate Fibonacci numbers using Stream ?
- Java – How to convert Roman letter/numbers to Integer ?
- Java – How to convert Celsius to Fahrenheit and vice-versa ?
References :
- https://karimsblog.medium.com/java-8-assigning-variable-inside-lambda-foreach-c73720f1860f
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
Happy Coding !!
Happy Learning !!