# convert from roman numerals to integers and vice versa

• 01-18-2010, 11:21 PM
number1cynic
convert from roman numerals to integers and vice versa
Hi! I have a question. I have to write a class that takes roman numerals and returns their integer value and vice versa. Any and all help would be greatly appreciated.

Here is what I have so far:

public class RomanNumerals
{
//Return the integer value of a roman numeral digit
private static int digitValue(char digit)
{
switch ( digit )
{
case 'I' : return 1;
case 'V' : return 5;
case 'X' : return 10;
case 'L' : return 50;
case 'C' : return 100;
case 'D' : return 500;
case 'M' : return 1000;
default : throw new RuntimeException("Not a valid Roman Numeral");
}
}
//Read a Roman Numeral string and return the integer value
public static int decimalValue(String roman)
{
int value = 0;
return value;
}

//Read a positive integer and return a string with its value in Roman numerals
public static String romanValue(int decimal)
{
String roman = "";
int N = number;
while (N >= 1000) {
// Move 1000 from N to roman.
roman += "M";
N -= 1000;
}
while (N >= 900) {
// Move 900 from N to roman.
roman += "CM";
N -= 900;
}
//etc
}
• 01-18-2010, 11:36 PM
Fubarable
Hello and welcome! First question is: what are you currently having problems with? What's not working?
• 01-18-2010, 11:58 PM
number1cynic
Hi, thank you for the warm welcome!

My question is... for the following method, I dont know where to begin:

public static int decimalValue(String roman)
{
int value = 0;
return value;
}
• 01-19-2010, 12:01 AM
AndreB
i sense much "if then else" branches in the java force

try to think like a roman. how your mind would process the roman digits ?
put that on a paper with a sate flow chart and you got yourself a draft.
• 01-19-2010, 12:07 AM
number1cynic
Ok I know that when the roman digits are strung together and a smaller digit is before a larger digit, we subtract. For example, IV = 5 -1 = 4... I also know that M = 1000, CM = 900, D = 500, CD = 400, C = 100, XC = 90, etc...

I just dont know how to translate this into code.
• 01-19-2010, 12:16 AM
AndreB
okay, before i can help you translate take a pensil and a paper and draw some kind of a state flow chart which covers (hopefully) every possible calculation.

if you have that you would have done 75% of the work. The rest is "easy" translate every branch from a state to an "if then [else if] else" statement.

i did such assignment in my first semester... well i remember it was very "dendritic" :)
• 01-19-2010, 12:39 AM
number1cynic
Is this what you mean?

M = 1000
CM = 900
CCM = 800
CCCM =700
CCCM = 600
D = 500
CD = 400
CCD = 300
CC = 200
C = 100
XC = 90
XXC = 80
XXXC = 70
LX = 60
L = 50
XL = 40
XXL = 30
XX = 20
X = 10
IX = 9
IIX = 8
IIIX =7
VI = 6
V = 5
IV = 4
III = 3
II =2
I = 1

Or should I write down all the cases from 1 to 100 ??

Also, thank u for working with me on this for so long ...
• 01-19-2010, 12:47 AM
AndreB
nah... i dont mean that. i want you to think about how you really transform roman numbers to integers

its late here, so i go to bed :-)

try to understand the following

Code:

```      Let arabic = 0       Let i = 0  // representing a position in the string             while i is a legal position in the string:           Let ch be the character in position i           Let N be the numeric equivalent of ch           i++  // to account for the character, ch           if there are no addition characters in the string:               Add N to arabic           else:  // Try pairing the ch with the next character               Let N2 be the numeric equivalent of the NEXT character               If N < N2:  // Evaluate the characters as a pair                   Add (N2 - N) to arabic                   i++    // to account for the extra character               else:                   Add N to arabic```
This is from another page; the citations will come later. i dont want spoil the work :-)
• 01-19-2010, 12:48 AM
Fubarable
Not quite. Say you are given a number 1974, describe the steps that you take to convert this by hand to Roman Numeral.

[edit: I'm a little late in getting this post off the block it seems]
• 01-19-2010, 12:50 AM
number1cynic
thanks for ur help
• 01-19-2010, 12:54 AM
number1cynic
Ok I think I got it now... If I were converting by hand from integers to roman numerals, I would first convert the largest digit.

For example, for 1974, I would first write down M (= 1000), then CM (= 900), then LXX (=70) and finally, IV (= 4)

1974 = MCMLXXIV