I'm coding a program that converts roman numerals into numbers. I've gotten to a certain point in my code where it keeps on showing the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at RomanNums.<init>(RomanNums.java:80)
at RomanNums.main(RomanNums.java:5)
Here is the code at which the problem occurs:
int num = IBIO.inputInt ("Which Roman Numeral would you like to convert? ");
int conv = 0;
//I for 1, V for 5, X for 10, L for 50, C for 100, D for 500, and M for 1000
if (num == 1){
for (int i = 0 ; i<n1.length ; i++)
{
if (n1[i]== 'I'){
if (n1[i+1] == 'V'){
conv+=4;
i++;
}
else if (n1[i+1] == 'X'){
conv+=9;
i++;
}
else
conv++;
}
if (n1[i] == 'V')
conv+=5;
if (n1[i] == 'X'){
if (n1[i+1] == 'L'){
conv+=40;
i++;
}
else if (n1[i+1] == 'C'){
conv+=90;
i++;
}
else
conv+=10;
}
if (n1[i] == 'L')
conv+=50;
if (n1[i] == 'C'){
if (n1[i+1] == 'D'){
conv+=400;
i++;
}
else if (n1[i+1] == 'M'){
conv+=900;
i++;
}
else
conv+=100;
}
if (n1[i] == 'D')
conv+=500;
if (n1[i] == 'M')
conv+=1000;
}
}
When I run the whole program works fine but once I input the number into int num, it doesn't work
Aucun commentaire:
Enregistrer un commentaire