1

I'm a new programming student and my assignment is to convert the input of a Roman Numeral to it's integer value. Here is what I have been given:

Write a program that converts a Roman number such as MCMLXXVIII to its decimal number representation. This program must have 3 methods and a main method!

  1. Write a method that takes input from the user and passes it to a conversion method.
  2. Write a method that yields the numeric value of each of the letters (conversion method).
  3. Write a method that outputs the number the user entered and the converted number.
  4. Write a main method to test the 3 methods.

HINT: Use a single dimensional array!

Convert a string as follows:

• Look at the first two characters. If the first has a larger value than the second, then simply convert the first.

• Call the conversion method again for the substring starting with the second character.

• Add both values. o If the first one has a smaller value than the second, compute the difference and add to it the conversion of the tail.

Now I am struggling trying to figure out what to do for my conversion method. Here is what I have written so far:

    public static String romanInput(String number) {

    Scanner numberInput = new Scanner (System.in);
    System.out.print("Enter a roman numeral: ");
    String userInput = numberInput.next();

    return userInput;
}

public static int numberConversion(int number) {

    int romanConv = 0;

    char[] romanChar = {1, 5, 10, 50, 100, 500, 1000};
    for (int i = 0; i < romanChar.length; i++)

}

You could see that I have already written the method that takes the input from a user. I think I did this correctly. However, I don't know what to do for this conversion method. It says to use a single dimensional array so that's what I did over here:

char[] romanChar = {1, 5, 10, 50, 100, 500, 1000};

Those are supposed to be the values of I, V, X, L, C, D, and M. I'm really just confused as where to go from there and I would appreciate it if someone can help me out.

NEPat10
  • 159
  • 1
  • 2
  • 12
  • This is not place to solve your homework, first try to think of an algorithm for conversion, post it here and then we can help you if you have specific problem. – Uhla Nov 08 '14 at 15:26
  • Not a duplicate so I would appreciate it if you don't google what I have posted because do you really think I would ask this question if I didn't already google it first myself? The problem with that question is I haven't learned about hashtables and enums so I can't be expected to use them. "This is not place to solve your homework". Really? You can't help a student in the right direction without guiding me all the way through it. Not asking for anyone to solve this assignment. – NEPat10 Nov 08 '14 at 15:36
  • Everyone asks for detailed questions so that's what I gave, and I get no help from anyone. – NEPat10 Nov 08 '14 at 15:36
  • There have been 3 similar postings today. I provide it code for one where I wrote an enum of base roman numerals (I, V, X, L, C, D, M) that return their respective value. What I will not do is write the algorithm to add these values together for an input such as the one on the OP's post. As Uhla stated, this is not a place where people do homework for students. IF we are to help, the very least they can do is show effort. – hfontanez Nov 08 '14 at 16:14

2 Answers2

1

The Roman numeration is non-positional, meaning that the value of the digits does not depend on their position and you can ignore the latter. Then it suffices to add the values of all digits.

There is an exception anyway: if a digit immediately precedes a digit of a higher value, then it is subtracted instead of added.

So the processing is simply:

  • Clear an accumulator.

  • Read the digits from left to right. For new every digit, convert it to its value and add it to the accumulator. In the end the accumulator contains the number value.

To handle the exception, you can use the following trick:

  • Use a variable that holds the value of the previous digit (initially set to the value of M);

  • when the current digit has a higher value than the previous, you must correct the accumulator by subtracting twice the value of the previous.

Programmatically:

( Initialize )
Prv= 1000
Acc= 0

Loop:
    ( Accumulate )
    Cur= Lookup(Digit[i])
    Acc+= Cur

    ( Adjust for inversions )
    if Prv < Cur -> Acc-= 2 * Prv
    Prv= Cur

For instance, CXIX gives

    Prv  Cur Acc
C   1000 100 100
X   100  10  110
I   10   1   111
X   1    10  121-2*1 = 119
  • There is a remaining challenge: sometimes you can find notations such as IIX (8), and the simple rule `Prv < Cur` will not suffice. –  Nov 08 '14 at 16:00
0

If I were you, I would begin by taking one baby step at a time. For example, if the only input I have to worry about is "I", then what? That is trivial of course.

Next, if the input is "II", then what? This suggests that I need to process the input one character at a time. Both the "I"s are equal to one each, and the result is the sum of the two. That means, I must have a "result" or some such variable, initialized to zero, and then for each character from the input string (I, then I), convert that to its numeric value (1, and then 1), add them up and return the value.

This logic works well for "III" also.

But then you face your first challenge with "IV". That is not trivial, specially if you are new to such an algorithm. Let me keep it aside, with a note that that is tough so will deal with this later.

The values "V", "VI", "VII", "VIII" all work fine with the above logic.

But then again I would be stuck with "IX". Similar to "IV" above. Maybe I have an idea about these two now, but then, maybe I'll still keep both these aside for the time being.

This works fine for "X", "XI", "XII", "XIII", and then again problem with "XIV".

I'll resist the temptation to solve the problems of "IV", "IX", "XIV" so that you can try them yourself; remember these are non-trivial, at least compared to what I have written above. Try it out.

So you see, incremental addition works well, but reduction is an unresolved problem.

Hope this helps.

Pradyumn
  • 400
  • 1
  • 2
  • 15