Java: Roman Numeral Tool
Posted by admin on
September 1, 2008
I was looking for a tool to check if a given String is a valid roman numeral or not. Remember old Math? Like IV = 4, or V = 5 or X1 = 11 and so on. I got lucky and chanced upon this link. I did a little modification to cater the code to my needs. This class has 2 public methods namely getRomanNumeral and isRomanNumeral.
getRomanNumeral takes in an int parameter and converts it to its roman numeral equivalent. If the value is less than 1 or greater than 3,999, it throws a NumberFormatException. isRomanNumeral meanwhile, takes in a String parameter and checks if the given data is a valid roman numeral or not. It’s a great helper class to handle roman numeral issues.
/*
An object of type RomanNumeral is an integer between 1 and 3999. It can
be constructed either from an integer or from a string that represents
a Roman numeral in this range. The function toString() will return a
standardized Roman numeral representation of the number. The function
toInt() will return the number as a value of type int.
*/
public class RomanNumeralTool {
private static int num; // The number represented by this Roman numeral.
/* The following arrays are used by the toString() function to construct
the standard Roman numeral representation of the number. For each i,
the number numbers[i] is represented by the corresponding string, letters[i].
*/
private static int[] numbers = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
private static String[] letters = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
public static String getRomanNumeral(int arabic) {
// Constructor. Creates the Roman number with the int value specified
// by the parameter. Throws a NumberFormatException if arabic is
// not in the range 1 to 3999 inclusive.
if (arabic < 1) throw new NumberFormatException("Value of RomanNumeral must be positive.");
if (arabic > 3999) throw new NumberFormatException("Value of RomanNumeral must be 3999 or less.");
num = arabic;
return convertIntToRomanNumeral();
}
public static boolean isRomanNumeral(String roman) {
boolean isRomanNumeral = true;
// Constructor. Creates the Roman number with the given representation.
// For example, RomanNumeral("xvii") is 17. If the parameter is not a
// legal Roman numeral, a NumberFormatException is thrown. Both upper and
// lower case letters are allowed.
if (roman.length() == 0) {
isRomanNumeral = false;
//throw new NumberFormatException("An empty string does not define a Roman numeral.");
}
roman = roman.toUpperCase(); // Convert to upper case letters.
int i = 0; // A position in the string, roman;
int arabic = 0; // Arabic numeral equivalent of the part of the string that has
// been converted so far.
while (i < roman.length()) {
char letter = roman.charAt(i); // Letter at current position in string.
int number = letterToNumber(letter); // Numerical equivalent of letter.
if (number < 0) {
isRomanNumeral = false;
//throw new NumberFormatException("Illegal character \"" + letter + "\" in roman numeral.");
}
i++; // Move on to next position in the string
if (i == roman.length()) {
// There is no letter in the string following the one we have just processed.
// So just add the number corresponding to the single letter to arabic.
arabic += number;
} else {
// Look at the next letter in the string. If it has a larger Roman numeral
// equivalent than number, then the two letters are counted together as
// a Roman numeral with value (nextNumber - number).
int nextNumber = letterToNumber(roman.charAt(i));
if (nextNumber > number) {
// Combine the two letters to get one value, and move on to next position in string.
arabic += (nextNumber - number);
i++;
} else {
// Don't combine the letters. Just add the value of the one letter onto the number.
arabic += number;
}
}
}
if (arabic > 3999) {
//throw new NumberFormatException("Roman numeral must have value 3999 or less.");
isRomanNumeral = false;
}
num = arabic;
return isRomanNumeral;
}
private static int letterToNumber(char letter) {
// Find the integer value of letter considered as a Roman numeral. Return
// -1 if letter is not a legal Roman numeral. The letter must be upper case.
switch (letter) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return -1;
}
}
private static String convertIntToRomanNumeral() {
// Return the standard representation of this Roman numeral.
String roman = ""; // The roman numeral.
int N = num; // N represents the part of num that still has
// to be converted to Roman numeral representation.
for (int i=0; i= numbers[i]) {
roman += letters[i];
N -= numbers[i];
}
}
return roman;
}
private static int toInt() {
// Return the value of this Roman numeral as an int.
return num;
}
}
Donations appreciated. Every little $ helps. Or click Google +1. Or play the short video on the upper right of this page.










2 Responses to “Java: Roman Numeral Tool”
Definitely agree with exactly what you wrote, perhaps others can learn from this. It was a pleasure visiting your site and I will add tech.chitgoks.com to my favorites right now.
By Impuscaturi on Mar 12, 2011
@Impuscaturi: thank you
By tech on Mar 12, 2011