0

I need to create a program for school where user inputs roman numbers and code outputs Arabic numbers. When I input V it outputs String index out of range: -1, but when I input I it works well, for context rimske = roman number, cisla = Arabic number

for (int i = 0; i <=rimske.length()-1 ; i++) {

        switch (rimske.charAt(i)){
            case 'I':
                cislo++;
                break;
            case 'V':
                cislo=cislo+5;
                if (rimske.charAt(i-1) == 'I')
                    cislo = cislo - 2;
                break;
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 1
    Please [edit] the question and include the source code as text, not as image. --- Please read: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Turing85 Dec 31 '20 at 12:49
  • On the first iteration `i` is 0 an you try to get index `i-1`, which is -1, so you may want to check whether `i` is not 0 before checking previous characters in the string. – Federico klez Culloca Dec 31 '20 at 12:51

0 Answers0