Convert Roman number to decimal

Asked

Viewed 2,052 times

4

I’m doing an exercise where I need to convert a typed Roman number, and the program returns a decimal number.

My algorithm:

import java.util.Scanner;

public class NumerosRomanos {

    public static void main (String args []) {

        System.out.println ("Digite um Número em Romanos, para saber qual é esse Número em Decimais");

        Scanner sc = new Scanner (System.in);

        String numeroRomanoInicial = sc.nextLine();

        String numeroRomano = numeroRomanoInicial.toUpperCase();

    System.out.println(verificarNumerosRomanos(numeroRomano));
    }

    public static int verificarNumerosRomanos (String numeroRomano) {
        int digitoRomano = 0;
        int digitoRomanoemconjunto = 0;

        for (int i=0; i<numeroRomano.length(); i++) {
        digitoRomano = numeroRomano.charAt(i);    
            switch (digitoRomano) {
                case 'I': digitoRomano += 1;
            break;
                case 'V': digitoRomano += 5;
            break;
                case 'X': digitoRomano += 10;
            break;
                case 'L': digitoRomano += 50;
            break;
                case 'C': digitoRomano += 100;
            break;
                case 'D': digitoRomano += 500;
            break;
                case 'M': digitoRomano += 1000;
            break;
            default: System.out.println("Dígito Romano Errado.");
            }
        }
            int soma = 0;

            for (int i = 0; i<numeroRomano.length(); i++) {

            char dr = numeroRomano.charAt(i);
            char drSeguinte = numeroRomano.charAt(i+1);

                if (dr == 'I' && drSeguinte == 'V') {
                    digitoRomanoemconjunto += 4;
                } else

                if (dr == 'I' && drSeguinte == 'X') {
                    digitoRomanoemconjunto += 9;
                }
                else

                if (dr == 'X' && drSeguinte == 'L') {
                    digitoRomanoemconjunto += 50;
                } else 

                if (dr == 'X' && drSeguinte == 'C') {
                    digitoRomanoemconjunto += 90;

                } else 

                if (dr == 'C' && drSeguinte == 'D') {
                    digitoRomanoemconjunto += 400;
                } else 

                if (dr == 'C' && drSeguinte == 'M') {
                   digitoRomanoemconjunto += 900;
                }
            }
            soma += digitoRomano + digitoRomanoemconjunto;
            return soma;
    }
}

Generates error of StringIndexOutOfBoundsException.

How to correct this error?

  • Can add the full error stack to the question as well?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

6

That mistake is on the line:

char drSeguinte = numeroRomano.charAt(i + 1);

That can solve with:

char drSeguinte = i < numeroRomano.length() - 1 ? numeroRomano.charAt(i + 1) : ' ';

It has way of making more efficient, but I go in the trivial to not confuse with new concept.

But the code has logic problems. What you are doing there is not much sense. The calculation is doing wrong, but this is another problem that is not the focus of the question.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thank you very much. You can answer what exactly is the problem of logic, and how to fix?

  • It’s not one, the whole code doesn’t do what it’s supposed to, so it doesn’t calculate properly. I can’t imagine why it has two loops, because there is this logic of the "following", at least the way it is does not generate the correct values.

  • Right. It really is a mistake. And how to make the correct code? You would have to use If s or Switch cases, right?

  • 2

    @Beginnerteemjava Yes, of course, but that’s another problem. This question is answered, if you have another problem you need to open another question. Here is a site of questions and objective responses. It’s not a forum, so every problem is a question. Take a look at [tour].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.