CODE
import java.util.Scanner;
public class RomanNum {
public static void main (String [] args) {
String [] ones = new String [10];
ones [0] = "";
ones [1] = "I";
ones [2] = "II";
ones [3] = "III";
ones [4] = "IV";
ones [5] = "V";
ones [6] = "VI";
ones [7] = "VII";
ones [8] = "VIII";
ones [9] = "IX";
String [] tens = new String [10];
tens [0] = "";
tens [1] = "X";
tens [2] = "XX";
tens [3] = "XXX";
tens [4] = "XL";
tens [5] = "L";
tens [6] = "LX";
tens [7] = "LXX";
tens [8] = "LXXX";
tens [9] = "XC";
String [] hundreds = new String [10];
hundreds [0] = "";
hundreds [1] = "C";
hundreds [2] = "CC";
hundreds [3] = "CCC";
hundreds [4] = "CD";
hundreds [5] = "D";
hundreds [6] = "DC";
hundreds [7] = "DCC";
hundreds [8] = "DCCC";
hundreds [9] = "CM";
String [] thousands = new String [4];
thousands [0] = "";
thousands [1] = "M";
thousands [2] = "MM";
thousands [3] = "MMM";
String [][] all = new String [4][];
all [0] = ones;
all [1] = tens;
all [2] = hundreds;
all [3] = thousands;
String roman = "";
Scanner input = new Scanner(System.in);
System.out.print("Enter a year, and I will convert it to roman numerals: ");
int year = input.nextInt();
String yearStr = Integer.toString(year);
int length = yearStr.length();
for (int i = 0; i < length; i++) {
roman += all[length - i - 1][Integer.valueOf(yearStr.substring(i,i+1))];
}
System.out.print(roman + "\n");
}
}
i have done this roman numeral problem but i saw the question wrong and i input integer number and output the roman numeral but it should've been the other way roman numeral should be a input and output should be a integer number
so i tried to change it but i couldn't figure out so can you help me out guys
here is the question:
Write a program that reads a string containing a Roman numeral representing a value in the range 1 to 3999. The program should print the Roman numeral and its value in our notation (a Hindu-Arabic numeral).
Roman numerals use the symbols: I = 1; V = 5; X = 10; L = 50; C = 100; D = 500; M = 1000.
Roman numerals use an additive rather than a positional notation. Numerals are formed by writing symbols from the preceding list, from left to right, to represent a sum, each time using the symbol for the largest possible value from the list of symbols. This rule is subject to the limitation that no symbol may be written more than 3 times in a row. To avoid this, the systems uses: IV = 4; IX = 9; XL = 40; XC = 90; CD = 400; and CM = 900. Input is from the keyboard, output to the screen.