Oliver, thanks for the reply.
I have a rough idea on switch statements, i have some books with
explanations so i can look over them, would you recommend having
different cases for each Numeral? Or which way of doing it would you
recommend?
I was going to have something like this before within the code, char c
represents the character the user has input.
static int convertRoman(char c)
{
if (c=='I') return 1;
if (c=='V') return 5;
if (c=='X') return 10;
if (c=='L') return 50;
if (c=='C') return 100;
if (c=='D') return 500;
if (c=='M') return 1000;
return 0;
}
[post re-ordered for clarity]
>> > Hi i'm fairly new to java and have been assigned to create a basic
>> > application which converts a roman numeral inputted by a user and then
[quoted text clipped - 12 lines]
>>
>> Do you know how to use a switch statement?
[...]
> static int convertRoman(char c)
> {
[quoted text clipped - 7 lines]
> return 0;
> }
Right, this is pretty much what I intended (although you used an if
statement instead of a switch statement). If you're familiar with
exceptions, it might be a good idea to throw an exception instead of
returning 0 if the input doesn't match any of the predefined letters. If
you're not familiar with exceptions, then don't worry about that for now, as
your code is fine as it is.
The next step is tricky, mainly because the roman numeral system has
some pretty strange rules. Is this for an official school assignment, or are
you just exploring on your own? If the latter, I recommend you pause this
task and try something easier, like converting an integer to its textual
representation. E.g. given the integer 1234, produce the string "one
thousand two hundred thirty four". Once you can do that, come back to this
roman numeral exercise.
- Oliver
ljs1987@hotmail.com - 28 Dec 2006 19:34 GMT
It is an assignment so yeah I need to get it done really, thanks for
the help though i'll just stick at it hopefully it'll come to me soon
enough.
Oliver Wong - 28 Dec 2006 21:37 GMT
> It is an assignment so yeah I need to get it done really, thanks for
> the help though i'll just stick at it hopefully it'll come to me soon
> enough.
If it were up to me, I'd try to formalize and articulate the rules for
converting roman numerals to numbers in plain English, and once I have it
all written down in English (or whatever natural language you're most
comfortable with), it'll be easier to try to translate that into Java.
For example, I might notice that If you have a series of Is, you can sum
them up, so that "I" is 1, "II" is 1+1=2, and so on. But if the Is appear
before a "bigger" character, their sum will get subtracted from that bigger
character, so IV is 5-1=4, or IX is 10-1=9.
This is an important step because if you don't even know what all the
rules are, there's no way you can possibly write a program that'll implement
all the rules.
- Oliver