Converting Roman Numerals into Integer Value in Java

Hi! coders today we are going to see that how to convert a Roman numeral in integer in Java. We all know that Roman numerals are the number system that originated in ancient Rome. These numbers are greatly found in ancient documents. Shakespeare used Roman numerals to index his plays. These types of numbers are represented with the help of alphabets. Modern usage employs seven symbols, each with fixed integer value:

SymbolIVXLCDM
Value1510501005001,000

Basic rules for writing roman numbers are:

1. If the smaller letter is written before the larger letter its get subtracted like IV = 5-1 =4

2. Similarly, if larger letter appears first it gets added to the smaller number like CL=100+50=150.

Hence we have tried to do the same through Java code:-

1. We have assigned the first letter as the sum for easy comparison.

2. Then we have compared the next letter with sum and its next letter. if found smaller than both it means it will get subtracted from its next letter and the result will get added to the sum. It will get jump twice.

3. If the number is smaller than sum or equal to sum and greater than its next letter than it will get added to the sum. It will jump to next letter only.

You may learn: Reverse individual words in a string or sentence in Java.

Here the code for same:

Program: Convert Roman Numerals to Integer Numbers in Java

import java.util.Scanner;

public class Roman_int {

public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
String st=sc.next();                  //input string
int len=st.length();
int sum=roman_int(st.charAt(0));

if(len==2)          //if length of string = 2 like IV or VI
{
if(roman_int(st.charAt(1))>sum)
sum=roman_int(st.charAt(1))-sum;
else
sum=sum+roman_int(st.charAt(1));

}
else {                  //length =1 or more than 2
int i=0;
        sum=0;
       
while(i<len-1)       //length = more than 2
{    
    if( roman_int(st.charAt(i))>= roman_int(st.charAt(i+1)))
  {
sum+=roman_int(st.charAt(i));
i++;
  }

    else
          {      
             sum+=roman_int(st.charAt(i+1))-roman_int(st.charAt(i));
             i+=2;
           }
   
}
if(i==len-1)              //for string len =1
   { sum+=roman_int(st.charAt(i));
       
   }
}


System.out.println(Math.abs(sum));
}

static int roman_int(char ch) {

int hi = 0;
switch(ch) {
case 'I':hi=1 ; break;
case 'V':hi=5 ; break;
case 'X':hi=10; break;
case 'L':hi=50; break;
case 'C':hi=100; break;
case 'D': hi=500;break;
case 'M': hi=1000; break;
default: System.out.println("invalid input");
         
}
return hi;
}


}

 

 

Output:

MMMDCCXCIV
3794

I hope you like the article.

3 responses to “Converting Roman Numerals into Integer Value in Java”

  1. M Lakshman Kumar says:

    This code is not working with string of sizes less than 3 and also when you give string that have small number at the end than previous char (ex: MMMDCV), we always get String Index Out of Bounds Exception.

Leave a Reply