0% found this document useful (0 votes)
2 views

Convert-Roman-Number-to-Decimal-_-String-Problem

The document presents a Java program that converts Roman numerals to decimal integers. It includes a method 'romanToInt' that processes each character of the Roman numeral string, applying specific rules for numeral combinations. The example provided converts the Roman numeral 'LVIII' to its decimal equivalent, 58.

Uploaded by

zakariaeechrigui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Convert-Roman-Number-to-Decimal-_-String-Problem

The document presents a Java program that converts Roman numerals to decimal integers. It includes a method 'romanToInt' that processes each character of the Roman numeral string, applying specific rules for numeral combinations. The example provided converts the Roman numeral 'LVIII' to its decimal equivalent, 58.

Uploaded by

zakariaeechrigui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

01/12/2020 Convert Roman Number to Decimal | String Problem

public class Main {

public static void main(String[] args) {


Main main = new Main();
int result = main.romanToInt("LVIII");
System.out.println(result);
}

/* Solution */
public int romanToInt(String s) {
char[] array = s.toCharArray();
int sum = 0;
for(int i=0; i<array.length; i++){

switch(array[i]){
case 'I':
if(i+1 < array.length){
if(array[i+1] == 'V'){
sum = sum + 4;
i++;
}else if(array[i+1] == 'X'){
sum = sum + 9;
i++;
}else {
sum = sum + 1;
}
}else {
sum = sum + 1;
}
break;
case 'V':
sum = sum + 5;
break;
case 'X':
if(i+1 < array.length){
if(array[i+1] == 'L'){
sum = sum + 40;
i++;
}else if(array[i+1] == 'C'){
sum = sum + 90;
i++;
}else {
sum = sum + 10;
}
}else {
sum = sum + 10;
}
break;
case 'L':
sum = sum + 50;
break;
case 'C':
if(i+1 < array.length){
if(array[i+1] == 'D'){
sum = sum + 400;
i++;
}else if(array[i+1] == 'M'){
sum = sum + 900;
i++;
}else {
sum = sum + 100;
}
}else {
sum = sum + 100;
}
break;
case 'D':
sum = sum + 500;
break;
https://codedestine.com/convert-roman-number-to-decimal-string-problem/ 1/2
01/12/2020 Convert Roman Number to Decimal | String Problem

case 'M':
sum = sum + 1000;
break;
}
}
return sum;
}
}

https://codedestine.com/convert-roman-number-to-decimal-string-problem/ 2/2

You might also like