/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class RomanToDecimalConverter {
private static int convertRec
(String s
) { if (s.isEmpty()) return 0;
if (s.startsWith("M")) return 1000 + convertRec(s.substring(1));
else if (s.startsWith("CM")) return 900 + convertRec(s.substring(2));
else if (s.startsWith("D")) return 500 + convertRec(s.substring(1));
else if (s.startsWith("CD")) return 400 + convertRec(s.substring(2));
else if (s.startsWith("C")) return 100 + convertRec(s.substring(1));
else if (s.startsWith("XC")) return 90 + convertRec(s.substring(2));
else if (s.startsWith("L")) return 50 + convertRec(s.substring(1));
else if (s.startsWith("XL")) return 40 + convertRec(s.substring(2));
else if (s.startsWith("X")) return 10 + convertRec(s.substring(1));
else if (s.startsWith("IX")) return 9 + convertRec(s.substring(2));
else if (s.startsWith("V")) return 5 + convertRec(s.substring(1));
else if (s.startsWith("IV")) return 4 + convertRec(s.substring(2));
else if (s.startsWith("I")) return 1 + convertRec(s.substring(1));
}
public static int convert
(String s
) { if (s == null || s.isEmpty() || !s.matches("^(M{0,3})(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$"))
return -1;
return convertRec(s);
}
public static void main
(String[] args
) { printRomanToDecimal("XCIX");
printRomanToDecimal("MMMCMXCIX");
//common error cases found in other solutions
printRomanToDecimal("IIX");
printRomanToDecimal("IIXX");
printRomanToDecimal("ABC");
printRomanToDecimal("IVX");
}
private static void printRomanToDecimal
(String s
) { //change the conversion method accordingly
int decimal = convert(s);
String output
= decimal
!= -1 ? ""+decimal
: "Invalid roman numerals"; System.
out.
println(s
+ ": " + output
); }
}