-1

私はこの String を持ってい9 $ -2 -ます。-の次が数字かどうかをテストしたい。

if (ch == '-') {
    if (ch != input.length() - 1) {
        char next = input.charAt(j + 1);
        if ((next >= '0' && next <= '9') || next == '.')
            temp = temp + ch;
    }
}

-文字列の最後にある可能性がある場合、それを行う方法が見つかりません

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 8
    at java.lang.String.charAt(String.java:658)
    at ParsePost.doParse(ParsePost.java:24)
    at InfixApp.main(InfixApp.java:20)
4

1 に答える 1

4

ch入力文字列内の文字、または文字のインデックス (位置) は何ですか? 最初の行は前者、2 行目は後者を想定しています。代わりに欲しいかもしれませんif (j != input.length() - 1)

于 2013-03-22T18:23:03.470 に答える