测试减去后的字符是否为数字

时间:2013-03-22 18:20:11

标签: java

我有这个字符串9 $ -2 -。我想测试下一个-是否为数字。

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

如果-可能位于String的最后一个

,我找不到怎么做
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)

1 个答案:

答案 0 :(得分:4)

什么是ch - 输入字符串中字符的字符或索引(位置)?第一行假定前者,第二行假定前者。也许你想要if (j != input.length() - 1)