// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class RomanToArabic {
    private static int decodeChar(char c) {
        switch(c) {
            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:
                throw new IllegalArgumentException(c + " is not a roman digit");
        }
    }
    public static int decodeString(String s) {
        int acc = 0;
        int ix = 0;
        while(ix < s.length()) {
            if(ix < s.length() - 1 && decodeChar(s.charAt(ix)) < decodeChar(s.charAt(ix + 1))) {
                acc -= decodeChar(s.charAt(ix));
            } else {
                acc += decodeChar(s.charAt(ix));
            }
            ix++;
        }
        return acc;
    }
    public static void main(String[] args) {
        System.out.println(decodeString("III"));
        System.out.println(decodeString("IV"));
        System.out.println(decodeString("V"));
        System.out.println(decodeString("VI"));
        System.out.println(decodeString("MCMXCIX"));
        System.out.println(decodeString("MMVII"));
    }
}
