Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need in java Write a program to convert roman numerals into their arabic equiv

ID: 3729107 • Letter: I

Question

I need in java

Write a program to convert roman numerals into their arabic equivalent. INPUT REQUIREMENTS Read one or more roman numerals from standard input. Process one line at a time. Each input line contains only one roman numeral, starting in column one. Assume the characters are all upper case with no embedded blanks. OUTPUT REQUIREMENTS The arabic equivalent of each input roman numeral is displayed on standard output, starting in column one. FUNCTIONAL REQUIREMENTS Here are the arabic equivalents for roman symbols: The "basic" roman symbols The "auxiliary" roman symbols I X C M V L D 1 10 100 1000 5 50 500 Convert the roman numeral to arabic processing the symbols from left to right according to the following rules: 1. A symbol following one of greater or equal value adds to its value. (E.g., XII = 12) 2. A symbol preceding one of greater value subtracts its value.(E.g., IV = 4; XL = 40) ERROR HANDLING REQUIREMENTS In each of the error conditions below, display the given message and skip the numeral and continue processing the next line. "Invalid character in input. Valid characters are I,V,X,L,C,D,M." Only the listed characters are valid. "Invalid numeral: can't subtract auxiliary symbol." It is not permitted to subtract an "auxiliary" symbol. (CML, not LM = 950; XLV not VL, = 45). "Invalid numeral: two consecutive subtractions." Can't do two subtractions in a row, thus LIVX is illegal. "Invalid numeral: additions don't decrease." Additions must decrease, as you go from left to right. Thus, each symbol added must have a value equal or less than the last symbol which was added. Thus, LIIX is wrong, cause we added L, added I, subtracted I, then try to add X.

Explanation / Answer

import java.util.Scanner;

class p7 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int[] numbers = { 1000, 500, 100, 50, 10, 5, 1 };
        String symbols = "MDCLXVI";

     
        System.out.print("Enter a roman numeral: ");
        final int MAX = 3999;
        Scanner keyb = new Scanner(System.in);
        String roman = keyb.next();
        keyb.close(); // don't want a resource leak

        roman = roman.toUpperCase();

        if (roman.matches(".*[0-9].*") || !roman.matches("[M|D|C|L|X|V|I]*")) {
            System.out.println("Impossible to convert. Wrong roman numeral");
        }

        int i = 0; // position in the Roman string

        int current = 0;
        int previous = 0;
        int arabic = 0;
        int flag=0,m=0;
        while (i < roman.length()) {

            char letter = roman.charAt(i); // letter at the current position in
                                            // the string

            // switch statement is easier to read than if - else if - else
            switch (letter) {
            case ('I'):
                current = 1;
                break;
            case ('V'):
                current = 5;
                break;
            case ('X'):
                current = 10;
                break;
            case ('L'):
                current = 50;
                break;
            case ('C'):
                current = 100;
                break;
            case ('D'):
                current = 500;
                break;
            case ('M'):
                current = 1000;
                break;
            }
            if (current > previous) {
                  arabic += current - (previous * 2);flag++;if(current>4)m=1;
            } else {
                 arabic += current;
            }

            previous = current;
            i += 1;
        }
if(flag<2 && m==0)
        System.out.println("Arabic: " + arabic);
        else
            System.out.println("Error");


    }
}

// I deployed some error handling mechanisms in this code . I hope it will be helpful . Thanks in advance