Here's my version of a Roman numeral to decimal number converter.
Here are the test results from one of my many tests.
Enter a Roman numeral: ccccllllxxxxvvvviiii
The input Roman numeral CCCCLLLLXXXXVVVVIIII is invalid.
The correct Roman numeral is DCLXIV.
The decimal value is 664.
Enter a Roman numeral: mcmlxxii
The input Roman numeral MCMLXXII is valid.
The decimal value is 1972.
Enter a Roman numeral: mmmccclll
The input Roman numeral MMMCCCLLL is invalid.
The correct Roman numeral is MMMCDL.
The decimal value is 3450.
Enter a Roman numeral: mcmlxxio
The input Roman numeral MCMLXXIO contains invalid characters.
Enter a Roman numeral: lcl
The input Roman numeral LCL is invalid.
The correct Roman numeral is CC.
The decimal value is 200.
Enter a Roman numeral: quit
Basically, I converted the input Roman numeral into a decimal number. Then I converted the decimal number back into a Roman numeral. I compared the Roman numerals, and output the correct Roman numeral along with the decimal value.
Here's the complete runnable code.
import java.util.Scanner;
public class RomanNumeralConversion {
public static void main(String[] args) {
RomanNumeralConversion rnc = new RomanNumeralConversion();
rnc.processRomanNumerals();
}
private Object[][] conversion = { { 1000, 900, 500, 400, 100, 90,
50, 40, 10, 9, 5, 4, 1 },
{ "M", "CM", "D", "CD", "C", "XC", "L", "XL",
"X", "IX", "V", "IV", "I" } };
public void processRomanNumerals() {
Scanner scanner = new Scanner(System.in);
String inputRomanNumeral = readRomanNumeral(scanner);
while (!inputRomanNumeral.equals("QUIT")) {
int value = convertToDecimal(inputRomanNumeral);
if (value < 0) {
System.out.println("The input Roman numeral " +
inputRomanNumeral + " contains invalid characters.");
} else {
String calculatedRomanNumeral = convertToRoman(value);
if (inputRomanNumeral.equals(calculatedRomanNumeral)) {
System.out.println("The input Roman numeral " +
inputRomanNumeral + " is valid.");
} else {
System.out.println("The input Roman numeral " +
inputRomanNumeral + " is invalid.");
System.out.println("The correct Roman numeral is " +
calculatedRomanNumeral + ".");
}
System.out.println("The decimal value is " + value + ".");
}
inputRomanNumeral = readRomanNumeral(scanner);
}
scanner.close();
}
private String readRomanNumeral(Scanner scanner) {
System.out.print("Enter a Roman numeral: ");
return scanner.nextLine().trim().toUpperCase();
}
private int convertToDecimal(String input) {
int output = 0;
int index = 0;
while (index < input.length()) {
boolean isInvalid = true;
for (int i = 0; i < conversion[1].length; i++) {
String test = (String) conversion[1][i];
int j = index + test.length();
if ((j <= input.length()) &&
(input.substring(index, j).equals(test))) {
output += (Integer) conversion[0][i];
index = j;
isInvalid = false;
break;
}
}
if (isInvalid) {
return -1;
}
}
return output;
}
private String convertToRoman(int input) {
String output = "";
for (int i = 0; i < conversion[0].length; i++) {
int value = (Integer) conversion[0][i];
if (input >= value) {
output += (String) conversion[1][i];
input -= value;
i--;
}
}
return output;
}
}