简体   繁体   中英

Validation of Roman Numerals! (Java)

I have written a Java program to convert roman numerals into numbers. My only problem is if somebody enters "IIII" it shows up as 4, but instead it should give an error that it is not a valid roman numeral. I need to include the following rules into my code. Could anybody help me with this?

(1) No digit is repeated in succession more than thrice, ie, I, X and C cannot be repeated more than 3 times.

(2) The digits V, L and D are not repeated. The repetition of V, L and D is invalid in the formation of numbers.

Conversion of Roman number to Decimal number:

public class RomanNumberUtils {
        static String romanNumeral;
        static int decimalNum;
        public static void main(String args[]) {
                RomanNumberUtils roman = new RomanNumberUtils();
                roman .convertRomanToDecimal();
                roman .printRoman(romanNumeral);
        }
 
        public void convertRomanToDecimal () {
                Scanner scan = new Scanner(System.in);
                System.out.print("Enter a Roman number: ");
                romanNumeral = scan.nextLine();
                romanNumeral = romanNumeral.toUpperCase();
                
                int l=  romanNumeral.length();
                int num=0;
                int previousnum = 0;
                for (int i=l-1;i>=0;i--)
                { 
                        char x =  romanNumeral.charAt(i);
                        x = Character.toUpperCase(x);
                        switch(x)
                        {  
                                case 'I':
                                previousnum = num;
                                num = 1;
                                break;
                             case 'V':
                                     previousnum = num;
                                num = 5;
                                break;
                                case 'X':
                                        previousnum = num;
                                num = 10;
                                break;
                                case 'L':
                                        previousnum = num;
                                num = 50;
                                break;
                                case 'C':
                                        previousnum = num;
                                num = 100;
                                break;
                                case 'D':
                                        previousnum = num;
                                num = 500;
                                break;
                                case 'M':
                                        previousnum = num;
                                num = 1000;
                                break;
                        }           
                        if (num<previousnum)
                        {decimalNum= decimalNum-num;}
                         else
                        decimalNum= decimalNum+num;
                }
        }
        public static void printRoman (String romanNumeral){
                System.out.println ("The equivalent of the Roman numeral "+romanNumeral+" is "+decimalNum);
        }
}

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;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM