Repository navigation
Expand file tree
/
Copy pathRomanToDigit.java
More file actions
75 lines (60 loc) · 1.92 KB
/
Copy pathRomanToDigit.java
File metadata and controls
75 lines (60 loc) · 1.92 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
class RomanToDigitOperation
{
private String romanString;
private int digitValue;
RomanToDigitOperation(String romaString)
{
this.romanString = romaString;
this.romanToNum();
System.out.println("The digit value is : "+digitValue);
}
int getDigitValue(char romanChar)
{
Map<Character , Integer> romanValueSet = new HashMap<>();
romanValueSet.put('I',1);
romanValueSet.put('V',5);
romanValueSet.put('X',10);
romanValueSet.put('L',50);
romanValueSet.put('C',100);
romanValueSet.put('D',500);
romanValueSet.put('M',1000);
return romanValueSet.get(Character.toUpperCase(romanChar));
}
void romanToNum()
{
for(int i=romanString.length() - 1;i>=1;i--)
{
if( (getDigitValue(romanString.charAt(i)) > getDigitValue(romanString.charAt(i-1))) )
{
digitValue += getDigitValue(romanString.charAt(i));
}
else if( (getDigitValue(romanString.charAt(i)) < getDigitValue(romanString.charAt(i+1))) )
{
digitValue -= getDigitValue(romanString.charAt(i));
}
}
if( (getDigitValue(romanString.charAt(0)) < getDigitValue(romanString.charAt(1))) )
{
digitValue -= getDigitValue(romanString.charAt(0));
}
else
{
digitValue += getDigitValue(romanString.charAt(0));
}
// digitValue += getDigitValue(romanString.charAt(romanString.length()-1));
}
}
public class RomanToDigit
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the roman : ");
String romanString = sc.nextLine();
new RomanToDigitOperation(romanString);
sc.close();
}
}