Java IndexOutOfBoundsException,无明确原因

Java IndexOutOfBoundsException,无明确原因,java,exception,indexoutofboundsexception,roman-numerals,Java,Exception,Indexoutofboundsexception,Roman Numerals,这可能是一个非常简单的问题,但我不知道哪里出了错。这是一个家庭作业问题,但我已经完成了逻辑,我只是在语法或其他方面遗漏了一个小细节,这导致了一些问题 import java.util.*; public class RomanNumeral{ private String romanInput; private int decimalValue; public boolean check; private int tester; private stat

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

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";
        }
    }
}
能不能给我一个指针,告诉我出了什么问题?我记得把我的索引限制在字符串的长度上,不确定我遗漏了什么

编辑:我认识到堆栈跟踪指向第61行。我在检查之前已经做了一行,以确保我小于正在索引的字符串的长度。我还可以检查什么才能使此工作正常?

这是您的问题:

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

问题是当i=input.length-1时

if (i < romanInput.length() - 1) {
    if(n < ((int)converter.get(romanInput.charAt(i+1)))){
您应该检查i是否小于romanInput.length-1

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

可能会重复添加一些println到代码中。你最终会明白的。java中的数组是基于零的。这就解决了这个问题。谢谢你的帮助,我没有任何进展。
if (i < romanInput.length() - 1) {
    if(n < ((int)converter.get(romanInput.charAt(i+1)))){