为什么我的" String索引超出范围"我运行这个程序时出错了吗?

时间:2014-11-12 01:36:45

标签: java

我正在创建一个程序,将罗马数字输入转换为它的整数值,每次运行程序时都会出现错误,

"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(String.java:646)
    at romannumeralconverter.RomanNumeralConverter.convert(RomanNumeralConverter.java:20)
    at romannumeralconverter.RomanNumeralConverter.romanInput(RomanNumeralConverter.java:68)
    at romannumeralconverter.RomanNumeralConverter.printValue(RomanNumeralConverter.java:72)
    at romannumeralconverter.RomanNumeralConverter.main(RomanNumeralConverter.java:77)
Java Result: 1"

现在我是编程的新手,所以我不知道这究竟意味着什么。我猜我的转换算法是错误的,其中输入的罗马数字不被循环读取。这就是我所拥有的:

public class RomanNumeralConverter {  

public String getUserInput() {

    Scanner numberInput = new Scanner (System.in);
    System.out.print("Enter a roman numeral in uppercase: ");
    String userInput = numberInput.next();
    numberInput.close();
    return userInput;
}

public int convert (String userInput) {
    int result = 0;
    int subtractamount = 0;
    int x = userInput.length();
    while(x != 0) {
    char romanConvert = userInput.charAt(x);
        if(x >= 1) {
        if(convertChar(romanConvert) >= convertChar(userInput.charAt(x - 1))) {
        subtractamount += convertChar(userInput.charAt(x - 1));
    }
}
    result += convertChar(romanConvert);
    x--;

}
result -= subtractamount;
return result;

}

public static char convertChar(char value) {
char result;
switch (value) {
    case 'I':
        result = 1;
        break;
    case 'V':
        result = 5;
        break;
    case 'X':
        result = 10;
        break;
    case 'L':
        result = 50;
        break;
    case 'C':
        result = 100;
        break;
    case 'D':
        result = 500;
        break;
    case 'M':
        result = 1000;
        break;
    default:
        System.out.println("Invalid character!");
        result = 0;
        break;
               }
return result;
}

public int romanInput() {       
    return convert(getUserInput());
}

public void printValue() {
System.out.println(romanInput());
}

public static void main (String[] args) {

new RomanNumeralConverter().printValue();
}
}

如果我的算法错误,有人知道如何修复它吗?

2 个答案:

答案 0 :(得分:1)

你应该从

开始
int x = userInput.length() - 1;

字符串中的最后一个字符位于索引 - (字符串长度 - 1),而不是字符串长度。

答案 1 :(得分:1)

userInput.charAt(x);更改为userInput.charAt(x - 1);

charAt starts with index 0 to length -1

int x = userInput.length() - 1;

@nd问题,一切都是0

您实际上在switch语句中使用大写字符

所以只需在函数convert(String userInput)

的开头添加以下语句
 userInput = userInput.toUpperCase(); // converts user input to uppercase , even if its is already or not.

public int convert(String userInput) {
        userInput = userInput.toUpperCase();
        int result = 0;
        int subtractamount = 0;
        int x = userInput.length() - 1;
        while (x != 0) {
            char romanConvert = userInput.charAt(x);
            if (x >= 1) {
                if (convertChar(romanConvert) >= convertChar(userInput.charAt(x - 1))) {
                    subtractamount += convertChar(userInput.charAt(x - 1));
                }
            }
            result += convertChar(romanConvert);
            x--;

        }
        result -= subtractamount;
        return result;

    }

<强>输出

Enter a roman numeral in uppercase: adig
Invalid character!
Invalid character!
Invalid character!
Invalid character!
501