Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2006

Tip: Looking for answers? Try searching our database.

a way of converting a roman numeral into an integer

Thread view: 
ljs1987@hotmail.com - 28 Dec 2006 14:53 GMT
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
outputs the corresponding number (integer). Any tips on how to do this
will be much appriciated.

I was thinking of doing an array for each numeral and number but again
i can't really work this out till i figure out how to convert the
numeral into an integer.

Thanks
Oliver Wong - 28 Dec 2006 15:23 GMT
> 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 - 4 lines]
> i can't really work this out till i figure out how to convert the
> numeral into an integer.

   Presumably, you mean you want to convert an ASCII representation of the
roman numerals, so that the 4-character string "VIII" represents 8, as
opposed to using the Unicode single character representation for roman
numeral eight.

   Do you know how to use a switch statement?

   - Oliver
ljs1987@hotmail.com - 28 Dec 2006 16:11 GMT
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;
    }

> > 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 - 13 lines]
>
>     - Oliver
Oliver Wong - 28 Dec 2006 18:34 GMT
[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
ljs1987@hotmail.com - 28 Dec 2006 16:11 GMT
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;
    }

> > 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 - 13 lines]
>
>     - Oliver
dimo414 - 29 Dec 2006 10:30 GMT
> 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 - 6 lines]
>
> Thanks

AP Computer Science is supposed to be for people who have at least some
idea what they're doing.  I'll admit some of the stuff that's covered
for the test is complicated, and people might need help (sorting and
searching come to mind).  But for something as simple as converting a
roman numeral to an integer, you should be ashamed of yourself.

Think about it this way: how do /you/ convert a roman numeral into an
arabic number?  Write down the rules that you use in your head, then
convert that to code.  Use some for loops, no arrays are needed.

Do your own homework from now on!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.