Dev Step
Dev Step

Reputation: 125

Convert roman numeral to integer

The roman numeral to integer converter I am following:

https://www.selftaughtjs.com/algorithm-sundays-converting-roman-numerals/

My attempt at converting the Javascript function to Java:

public class RomanToDecimal {
public static void main (String[] args) {

    int result = 0;
    int[] decimal = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

    // Test string, the number 895
    String test = "DCCCXCV";

    for (int i = 0; i < decimal.length; i++ ) {
        while (test.indexOf(roman[i]) == 0) {
            result += decimal[i];
            test = test.replace(roman[i], "");
        }
    }
    System.out.println(result);
}

}

The output is 615, which is incorrect.

Please help me understand where I went wrong.

Upvotes: 4

Views: 5198

Answers (3)

12201129
12201129

Reputation: 1

def value(r): if (r == 'I'): return 1 if (r == 'V'): return 5 if (r == 'X'): return 10 if (r == 'L'): return 50 if (r == 'C'): return 100 if (r == 'D'): return 500 if (r == 'M'): return 1000 return -1

def romanToDecimal(str): res = 0 i = 0

while (i < len(str)):

    # Getting value of symbol s[i]
    s1 = value(str[i])

    if (i + 1 < len(str)):

        # Getting value of symbol s[i + 1]
        s2 = value(str[i + 1])

        # Comparing both values
        if (s1 >= s2):

            # Value of current symbol is greater
            # or equal to the next symbol
            res = res + s1
            i = i + 1
        else:

            # Value of current symbol is greater
            # or equal to the next symbol
            res = res + s2 - s1
            i = i + 2
    else:
        res = res + s1
        i = i + 1

Upvotes: 0

Eran
Eran

Reputation: 393936

Your test = test.replace(roman[i], ""); replaces all occurrences of "C" with "", so after you find the first "C" and add 100 to the total, you eliminate all the remaining "C"s, and never count them. Therefore you actually compute the value of "DCXV", which is 615.

You should only replace the occurrence of roman[i] whose start index 0, which you can achieve by replacing:

test = test.replace(roman[i], "");

with:

test = test.substring(roman[i].length()); // this will remove the first 1 or 2 characters
                                          // of test, depending on the length of roman[i]

The following:

int result = 0;
int[] decimal = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

// Test string, the number 895
String test = "DCCCXCV";

for (int i = 0; i < decimal.length; i++ ) {
    while (test.indexOf(roman[i]) == 0) {
        result += decimal[i];
        test = test.substring(roman[i].length());
    }
}
System.out.println(result);

prints:

895

Upvotes: 7

Patrick Parker
Patrick Parker

Reputation: 4957

test = test.replace(roman[i], "");

This will replace every occurrence. Instead, you should only truncate off the occurrence at the beginning of the string (position 0).

Try using substring instead of replace, and pass as an argument the length of roman[i]

Upvotes: 1

Related Questions