Question
I am trying to convert roman numerals into regular numbers using java. I am using intellij for my ide. Please proof my code so that
I am trying to convert roman numerals into regular numbers using java. I am using intellij for my ide. Please proof my code so that it may run.
public class RomanNumeralsToNumbers {
public static void main(string[] args) {
string str = "DCCVII"; int len = str.lenth(); str = str + " "; int result = 0; for (int i = 0; i < len; i++) { //for loop char ch = str.charAt(i); char next_char = str.char.At(i + 1); //next roman numeral if (ch == 'M') { result += 1000; } else if (ch == 'C') { if (next_char == 'M') { result += 900; i++; } else if (next_char == 'D') { //next roman numeral result += 400; i++; } else { result += 100; } else if (ch == 'X') { if (next_char == 'C') { result += 90; //result of X and C i++; } else if (next_char == 'L') { result += 40; i++; } else { result += 10; } } else if (ch == 'L') { result += 50; } else if (ch == 'I') { if (next_char == 'X') { result += 9; i++; } else if (next_char == 'V') { result += 4; i++; } else { result++; } } else { result += 5; } System.out.println(" Roman Number: " + str); System.out.println("Equivalent integer: " + result + " "); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started