-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathRomantoInt.java
36 lines (31 loc) · 1.3 KB
/
RomantoInt.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.*;
public class RomantoInt {
public static int romanToInt(String s) {
// Create a mapping of Roman numerals to their corresponding values
HashMap<Character, Integer> romanToInteger = new HashMap<>();
romanToInteger.put('I', 1);
romanToInteger.put('V', 5);
romanToInteger.put('X', 10);
romanToInteger.put('L', 50);
romanToInteger.put('C', 100);
romanToInteger.put('D', 500);
romanToInteger.put('M', 1000);
int result = 0;
// Iterate through the Roman numeral string
for (int i = 0; i < s.length(); i++) {
int currentVal = romanToInteger.get(s.charAt(i));
// If the current numeral is smaller than the next numeral, subtract it
if (i < s.length() - 1 && currentVal < romanToInteger.get(s.charAt(i + 1))) {
result -= currentVal;
} else {
result += currentVal;
}
}
return result;
}
public static void main(String[] args) {
String romanNumeral = "MCMXCIV"; // Replace with the Roman numeral you want to convert
int integer = romanToInt(romanNumeral);
System.out.println("The Roman numeral " + romanNumeral + " is equivalent to " + integer + " in integer form.");
}
}