0

I'm doing a roman numeral converter to decimal from 1 to 999, I can add the value to acum, for example XV, I can add 10+5=15 but I can't subtract like IV, 5-1=4, how can I compare if a letter is before with a lower value? I tried to print an array backwards to compare but I couldn't.

import java.util.*;

public class NumerosRomanos {
    private String numero;

    public NumerosRomanos(String numero){
        setNumero(numero);
    }
        public void setNumero(String numero){
            this.numero=numero;
        }
        public String getNumero(){
            return numero;
        }

        public String mayusculas(String numero){
            return numero.toUpperCase();
    }

    public String separar(){
        String mayusculas=mayusculas(numero);
        String[] resultado=mayusculas.split("",0);
        for(int i=0;i<resultado.length;i++){
            System.out.println(resultado[i]);
        }
        int acum=0;
        for(int i=0;i<resultado.length;i++){
        switch(resultado[i]){
        case "I": acum+=1;
        break;
        case "V": acum+=5;
        break;
        case "X": acum+=10;
        break;
        case "L": acum+=50;
        break;
        case "C": acum+=100;
        break;
        case "D": acum+=500;
        break;
        case "M": acum+=1000;
        break;
        default: System.out.println("Error");
        break;
        }
        }
        for(int contador2=resultado.length; contador2 > 0;contador2--){
            System.out.println(resultado[contador2]);
        }
        String ultimo= Integer.toString(acum);
        return ultimo;
    }




    public String toString(){
        return separar();
    }

}
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71

1 Answers1

0

Someone has already written a beautiful solution for the same. this might help you. take a look. https://stackoverflow.com/a/9073310/1388715

Community
  • 1
  • 1
Amulya Holla
  • 83
  • 1
  • 7