# Thread: convert from roman numerals to integers and vice versa

1. Member Join Date
Jan 2010
Posts
6
Rep Power
0

## 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
}  Reply With Quote

2. ## Hello and welcome! First question is: what are you currently having problems with? What's not working?  Reply With Quote

3. Member Join Date
Jan 2010
Posts
6
Rep Power
0

## 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;
}  Reply With Quote

4. ## 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.  Reply With Quote

5. Member Join Date
Jan 2010
Posts
6
Rep Power
0

## 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.  Reply With Quote

6. ## 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" :)  Reply With Quote

7. Member Join Date
Jan 2010
Posts
6
Rep Power
0

## 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 ...  Reply With Quote

8. ## 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

Java 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:
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:
This is from another page; the citations will come later. i dont want spoil the work :-)  Reply With Quote

9. ## 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]  Reply With Quote

10. Member Join Date
Jan 2010
Posts
6
Rep Power
0

## thanks for ur help  Reply With Quote

11. Member Join Date
Jan 2010
Posts
6
Rep Power
0

## 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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•