zamzam zamzam - 4 months ago 10
Java Question

String index out of bound error

The bellow class contain a method

fromRomanToArabic ()
in this method I'm trying to convert a Arabic number to Roman number. the method should act as follows:


  • take the user String from the object

  • itterate through the string

  • add up the values associated with the characters. If the characters associated number is larger than the previous one, deduct the character value from the sum.



here is my code:

package romantoarabicnums;

import javax.swing.JOptionPane;


public class RomanToArabicNums {
private static String userString;
private static int userNumber;
private static char letter;
private static int letterNum;

public RomanToArabicNums (String s) throws NumberFormatException {
for (int i = 0; i < s.length(); i++) {

if (s.charAt(i) == 'M' ||s.charAt(i) == 'D' ||s.charAt(i) == 'C' ||s.charAt(i) == 'X' ||
s.charAt(i) == 'V' ||s.charAt(i) == 'I' ||s.charAt(i) == 'L') {
userString += s.charAt(i);
}
else {throw new NumberFormatException("Only M,C,D,X,V and I allowed");}
}
}
public RomanToArabicNums (int num) throws NullPointerException {
if (num >= 1 && num <= 3999) {
RomanToArabicNums.userNumber = num;
}
else {throw new NumberFormatException("numbers between 1 and 3999 only!");}
}
public RomanToArabicNums (char let, int num) {
RomanToArabicNums.letter = let;
RomanToArabicNums.letterNum = num;
}
public static void main(String[] args) {
RomanToArabicNums r2 = new RomanToArabicNums("MCMXCV");
System.out.println("the arabic number is: " + r2.fromRomanToArabic());

}
/* convert Roman characters to Arabic numbers */
public int fromRomanToArabic () {
int sum = 0;

for (int i = 0; i <= userString.length(); i++) {
/**/
if (userString.charAt(i) == 'M') {
sum += 1000;
}
/**/
if (userString.charAt(i) == 'D') {
if (userString.charAt(i + 1) == 'M') {
sum -= 500;
} else {
sum += 500;
}
}
/**/
if (userString.charAt(i) == 'C') {
if (userString.charAt(i + 1) == 'M' || userString.charAt(i + 1) == 'D') {
sum -= 100;
} else {
sum += 100;
}
}
/**/
if (userString.charAt(i) == 'L') {
if (userString.charAt(i + 1) == 'M' || userString.charAt(i + 1) == 'D'
|| userString.charAt(i + 1) == 'C') {
sum -= 50;
} else {
sum += 50;
}
}
/**/
if (userString.charAt(i) == 'X') {
if (userString.charAt(i + 1) == 'M' || userString.charAt(i + 1) == 'D'
|| userString.charAt(i + 1) == 'C' || userString.charAt(i + 1) == 'L') {
sum -= 10;
} else {
sum += 10;
}
}
/**/
if (userString.charAt(i) == 'V') {
if (userString.charAt(i + 1) == 'M' || userString.charAt(i + 1) == 'D'
|| userString.charAt(i + 1) == 'C' || userString.charAt(i + 1) == 'L'
|| userString.charAt(i + 1) == 'X') {
sum -= 5;
} else {
sum += 5;
}
}
/**/
if (userString.charAt(i) == 'I') {
if (userString.charAt(i + 1) == 'M' || userString.charAt(i + 1) == 'D'
|| userString.charAt(i + 1) == 'C' || userString.charAt(i + 1) == 'L'
|| userString.charAt(i + 1) == 'X' || userString.charAt(i + 1) == 'V') {
sum -= 1;
} else {
sum += 1;
}
}
}
return sum;

}
}


and here is the error:
enter image description here

NOTE: I think that the error is due to number of string elements being less that the steps in the for loop

eol eol
Answer

In addition to Jens' and Andy's answers you should actually check for i + 1 < userString.length(), since you use userString.charAt(i + 1) == 'M'.

This will also cause an IndexOutOfBoundsException if you're at the index of the last character.

Note that you'll need to handle the last character accordingly or keep i < userString.length() and add a check if i + 1 exceeds the string's length wherever you use userString.charAt(i + 1).