Can someone please help me doing this JAVA code. Write a program to accept a rom
ID: 3707611 • Letter: C
Question
Can someone please help me doing this JAVA code.
Write a program to accept a roman numeral and determine if it is valid. You do not need to convert the numeral, just determine if it is valid. This is based on the problem from https://projecteuler.net/problem=89. You can go there for more information. and a set of roman numeral samples.
Modify the above so that the user enters a file name containing roman numerals. For each roman numeral in the file print out that numeral and a determination of validity (VALID/NOT VALID).
Explanation / Answer
public class RomanValidator {
public static boolean isValidAndBestWayToRoman(String input) {
boolean isValid = false;
if(input == null || input.isEmpty()) {
isValid = false;
} else {
// Convert to upper case letters for simplicity
input = input.toUpperCase();
// The following is a regex expression.
// It can be broken as follows
// M- upto 3 times at start
// CM- C before M once, CD- C before D once, D- D once ?C{0,3} -- C after previous 3 upto 3 times.
// -- Rest are similar in next order
isValid = input.matches("M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})");
}
return isValid;
}
public static void checkAndPrintValidityOfRoman(String input) {
if(isValidAndBestWayToRoman(input)){
System.out.println(input + " is VALID");
} else {
System.out.println(input + " is NOT VALID");
}
}
public static void main(String[] args) {
checkAndPrintValidityOfRoman("XVI");
checkAndPrintValidityOfRoman("ABC"); // invalid roman characters
checkAndPrintValidityOfRoman("IVX"); // improper way to write
checkAndPrintValidityOfRoman("MDCLXXXVII");
}
}