Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roman Numeral to Number Conversion [duplicate]

Tags:

java

Trying to write program to read in a string of characters that represent a Roman numeral (from user input) and then convert it to Arabic form (an integer). For instance, I = 1, V = 5, X = 10 etc.

Basically, the constructor that takes a parameter of type String must interpret the string (from user input) as a Roman numeral and convert it to the corresponding int value.

Is there an easier way to solve this besides the below in progress (which isn't compiling as yet):

import java.util.Scanner;

public class RomInt {
String roman;
int val;
void assign(String k)
{
  roman=k;
}

private class Literal
{
    public char literal;
    public int value;

    public Literal(char literal, int value)
    {
        this.literal = literal;
        this.value = value;
    }
}

private final Literal[] ROMAN_LITERALS = new Literal[]
        {
                new Literal('I', 1),
                new Literal('V', 5),
                new Literal('X', 10),
                new Literal('L', 50),
                new Literal('C', 100),
                new Literal('D', 500),
                new Literal('M', 1000)
        };

public int getVal(String s) {

   int holdValue=0;

        for (int j = 0; j < ROMAN_LITERALS.length; j++)
        {
            if (s.charAt(0)==ROMAN_LITERALS[j].literal)
            {
                       holdValue=ROMAN_LITERALS[j].value;
                           break;
            }  //if()
        }//for()

  return holdValue;
}  //getVal()
public int count()
{
   int count=0;
   int countA=0;
   int countB=0;
   int lastPosition = 0;
    for(int i = 0 ; i < roman.length(); i++)
    {
      String s1 = roman.substring(i,i+1);
        int a=getVal(s1);
        countA+=a;
    }
    for(int j=1;j<roman.length();j++)
    {
        String s2=  roman.substring(j,j+1);
        String s3=  roman.substring(j-1,j);
        int b=getVal(s2);
        int c=getVal(s3);
        if(b>c)
        {
            countB+=c;
        }
    }
    count=countA-(2*countB);
    return count;
    }


void disp()
{

     int result=count();
    System.out.println("Integer equivalent of "+roman+" = " +result);
}


  public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter Roman Symbol:");
String s = keyboard.nextLine();
RomInt();

}

}  
like image 333
user3003605 Avatar asked Oct 15 '25 17:10

user3003605


2 Answers

Roman numerals/Decode Example:

class Roman {

    private static int decodeSingle(char letter) {
        switch (letter) {
            case 'M':
                return 1000;
            case 'D':
                return 500;
            case 'C':
                return 100;
            case 'L':
                return 50;
            case 'X':
                return 10;
            case 'V':
                return 5;
            case 'I':
                return 1;
            default:
                return 0;
        }
    }

    public static int decode(String roman) {
        int result = 0;
        String uRoman = roman.toUpperCase(); //case-insensitive
        for (int i = 0; i < uRoman.length() - 1; i++) {//loop over all but the last character
            if (decodeSingle(uRoman.charAt(i)) < decodeSingle(uRoman.charAt(i + 1))) {
                result -= decodeSingle(uRoman.charAt(i));
            } else {
                result += decodeSingle(uRoman.charAt(i));
            }
        }
        result += decodeSingle(uRoman.charAt(uRoman.length() - 1));
        return result;
    }

    public static void main(String[] args) {
        System.out.println(decode("MCMXC")); //1990
        System.out.println(decode("MMVIII")); //2008
        System.out.println(decode("MDCLXVI")); //1666
    }
}
like image 88
Alya'a Gamal Avatar answered Oct 18 '25 07:10

Alya'a Gamal


Use enum, for easy and simple solution. At first define the decimal equivalent weight at roman.

enum Roman{
    i(1),iv(4),v(5), ix(9), x(10);
    int weight;

    private Roman(int weight) {
        this.weight = weight;
    }
};

This is the method to convert decimal to roman String.

static String decToRoman(int dec){
    String roman="";
    Roman[] values=Roman.values();
    for (int i = values.length-1; i>=0; i--) {
       while(dec>=values[i].weight){
           roman+=values[i];
           dec=dec-values[i].weight;
       }            
    }
    return roman;
}
like image 43
Masudul Avatar answered Oct 18 '25 09:10

Masudul