user6217303 user6217303 - 7 months ago 13
Java Question

Converting Decimal to Roman Numeral in Java?

I'm really new to computer science and I know that I'm most likely making very simple mistakes right now. I'm making a program to convert numbers to roman numerals and my 'Previous' variable just isn't working.

Edit for clarification:
Every numeral combination after V (X,L,C,D,M,) are over the mark. Also, my output treats input the same forward and backward. Ex:(IV=6)

Here is my code:

import java.util.*;

public class roman
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a Roman numeral: ");
String R = in.nextLine();
System.out.println("Converted to Decimal: " + RomanToDecimal(R));
}

static int RomanToDecimal(String R)
{
int Decimal = 0;
char Previous;

for (int x = 0; x < R.length(); x++)
{
Previous = R.charAt(x);
if(R.charAt(x) == 'I')
Decimal += 1;

if(R.charAt(x) == 'V')
{
if (Previous == 'I')
Decimal -= 1;
else
Decimal += 5;
}

if(R.charAt(x) == 'X')
{
if (Previous == 'I')
Decimal-= 1;
else
Decimal+= 10;
}

if(R.charAt(x) == 'L')
{
if (Previous == 'X')
Decimal -= 10;
else
Decimal+=50;
}

if(R.charAt(x) == 'C')
{
if (Previous == 'X')
Decimal -= 10;
else
Decimal += 100;
}

if(R.charAt(x) == 'D')
{
if (Previous == 'C')
Decimal -= 100;
else
Decimal += 500;
}

if(R.charAt(x) == 'M')
{
if (Previous == 'C')
Decimal -= 100;
else
Decimal+= 1000;
}

Previous = R.charAt(x);
}

return Decimal;
}
}

Answer

This is the code corrected:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a Roman numeral: ");
        String R = in.nextLine();
        System.out.println("Converted to Decimal: " + RomanToDecimal(R));
    }

    static int RomanToDecimal(String R) {
        int Decimal = 0;
        char Previous = 0;

        for (int x = 0; x < R.length(); x++) {
            if (R.charAt(x) == 'I')
                Decimal += 1;

            if (R.charAt(x) == 'V') {
                System.out.println(Previous);
                if (Previous == 'I') {
                    Decimal -= 2;
                }
                Decimal += 5;
            }

            if (R.charAt(x) == 'X') {
                if (Previous == 'I') {
                    Decimal -= 2;
                }
                Decimal += 10;
            }

            if (R.charAt(x) == 'L') {
                if (Previous == 'X') {
                    Decimal -= 20;
                }
                Decimal += 50;
            }

            if (R.charAt(x) == 'C') {
                if (Previous == 'X') {
                    Decimal -= 20;
                }
                Decimal += 100;
            }

            if (R.charAt(x) == 'D') {
                if (Previous == 'C') {
                    Decimal -= 200;
                }
                Decimal += 500;
            }

            if (R.charAt(x) == 'M') {
                if (Previous == 'C') {
                    Decimal -= 200;
                }
                Decimal += 1000;
            }
            Previous = R.charAt(x);
        }
        return Decimal;

    }
}

You weren't counting well on the behaviour when Previous character appears, not only you want to discount the previous added but you want to discount the same number from the next character so needs to be discounted twice.

Comments