Results 1 to 11 of 11
 01182010, 10:21 PM #1Member
 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
}

Hello and welcome! First question is: what are you currently having problems with? What's not working?
 01182010, 10:58 PM #3Member
 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;
}
 01182010, 11:01 PM #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."There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
 01182010, 11:07 PM #5Member
 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.
 01182010, 11:16 PM #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" :)"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
 01182010, 11:39 PM #7Member
 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 ...
 01182010, 11:47 PM #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: 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
"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"

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]
 01182010, 11:50 PM #10Member
 Join Date
 Jan 2010
 Posts
 6
 Rep Power
 0
thanks for ur help
 01182010, 11:54 PM #11Member
 Join Date
 Jan 2010
 Posts
 6
 Rep Power
 0
Similar Threads

Convert roman numerals
By matzahboy in forum New To JavaReplies: 4Last Post: 02212010, 10:06 PM 
Retrieving all the keys corresponding to a value and vice versa
By Ms.Ranjan in forum New To JavaReplies: 5Last Post: 04162009, 06:30 PM 
Postfix into prefix and vice versa
By sfe23 in forum New To JavaReplies: 9Last Post: 02192009, 10:37 PM 
Converting to ASCII and viceversa
By pheonix in forum New To JavaReplies: 2Last Post: 09092008, 04:43 AM 
Convert roman numerals to integers
By Felissa in forum Advanced JavaReplies: 2Last Post: 07012007, 11:27 PM
Bookmarks