roman numbers to numbers

Solutions on MaxInterview for roman numbers to numbers by the best coders in the world

showing results for - "roman numbers to numbers"
Isabel
09 Jul 2020
1//Java Implementation of Roman To Number
2
3
4public class RomanToNumber {
5	public static int declareIntOfChar(char c)
6	{
7		int val=0;
8		switch(c)
9		{
10		case 'I':
11			val=1;
12			break;
13		case 'V':
14			val=5;
15			break;
16		case 'X':
17			val=10;
18			break;
19		case 'L' : 
20			val=50;
21			break;
22		case 'C' : 
23			val=100;
24			break;
25		case 'D' : 
26			val=500;
27			break;
28		case 'M' : 
29			val=1000;
30			break;
31		default :
32			val=-1;	
33			break;
34		}
35		return val;
36	}
37	public static void main(String[] args) {
38		String s = "XCV";
39		int sum = 0,c1,c2;
40		for(int i=0;i<s.length();i++)
41		{
42			c1=declareIntOfChar(s.charAt(i));
43			if(i+1<s.length())
44			{
45				c2=declareIntOfChar(s.charAt(i+1));
46				if(c1<c2)
47				{
48					sum = sum + c2 - c1;
49					i++;
50				}
51				else
52				{
53					sum = sum + c1;
54				}
55			}
56			else
57			{
58				sum = sum + c1;
59			}
60		}
61		System.out.print(s + " = " + sum);
62	}
63}
64