IndexOutOfBoundsException,没有明确的原因

IndexOutOfBoundsException, no clear cause

这可能是一个非常简单的问题,但我想不出我哪里错了。这是一个家庭作业问题,但我已经完成了逻辑,我似乎只是在我的语法或其他方面遗漏了一个小细节,这导致了问题。

import java.util.*;

public class RomanNumeral{
    private String romanInput;
    private int decimalValue;
    public boolean check;
    private int tester;
    private static final Hashtable<Character, Integer> converter = new Hashtable<Character, Integer>();{
    RomanNumeral.converter.put('I',1);
    RomanNumeral.converter.put('V',5);
    RomanNumeral.converter.put('X',10);
    RomanNumeral.converter.put('L',50);
    RomanNumeral.converter.put('C',100);
    RomanNumeral.converter.put('D',500);
    RomanNumeral.converter.put('M',1000);
    }

    public RomanNumeral(String romanInput){
    this.romanInput = romanInput;
    }

    /**
     * @return the roman numeral
     */
    public String getRoman() {
        convertRoman(romanInput);
        return romanInput;
    }

    /**
     * @return the decimal form of the numeral
     */
    public int getDecimal() {
        convertRoman(romanInput);
        return decimalValue;
    }

    private void isValid(String romanInput){
        tester=0;
        for(int i = 0; i < romanInput.length(); i++) { //this test detects invalid characters
            char letter = romanInput.charAt(i);
            if(converter.containsKey(letter)){
                tester += 1;
            }
            else {
            }
        }
        check = (tester==romanInput.length());

    }

    //go character by character to convert the number (converter.lookup() would be better, but requires all cases be present in the Hash table (as far as I know))
    private void convertRoman(String romanInput){
        isValid(romanInput);
        if(check){
            decimalValue = 0;
            for(int i = 0; i < romanInput.length(); i++) {
                char letter = romanInput.charAt(i);
                int n = (int) converter.get(letter);
                if(i < romanInput.length()){
                    if(n < ((int)converter.get(romanInput.charAt(i+1)))){
                        decimalValue -= n;
                    }
                    else {
                        decimalValue += n;
                    }
                }
                else {
                    decimalValue += n;
                }
            }
        }
        else{
            decimalValue = 0;
            this.romanInput = "Invalid Roman Numeral";
        }
    }
}

当我 运行 此代码使用 getRoman("IV") 或任何其他罗马数字时,我得到以下信息:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(Unknown Source)
    at RomanNumeral.convertRoman(RomanNumeral.java:61)
    at RomanNumeral.getRoman(RomanNumeral.java:26)

我可以指点一下哪里出了问题吗?我记得将我的索引限制为字符串的长度,不知道我错过了什么。

编辑:我知道我的堆栈跟踪指向第 61 行。我已经在检查之前创建了该行以确保 i 小于我正在索引的字符串的长度。我还可以检查什么来使这项工作正常进行?

这是您的问题:

for(int i = 0; i < romanInput.length(); i++) {
   ...
   if(i < romanInput.length()){
       if(n < ((int)converter.get(romanInput.charAt(i+1)))){
           decimalValue -= n;
       }

您尝试访问 romanInput.charAt(i+1),但该索引可能不存在,因为您只确保 i < romanInput.length()。我想你需要的是

for(int i = 0; i < romanInput.length(); i++) {
   ...
    if(i < romanInput.length() -1){ // here -1!!
        if(n < ((int)converter.get(romanInput.charAt(i+1)))){

问题是当 i= romanInput.length() - 1

你应该检查 i 是否小于 romanInput.length() - 1

if (i < romanInput.length() - 1) {
    if(n < ((int)converter.get(romanInput.charAt(i+1)))){