0

不热衷于使用该parseInteger解决方案,它很丑陋,正如 Joshua Bloch 所说,您应该“仅在异常情况下使用异常”。当然,我可以使用下面的代码块之类的东西,但不能保证它是整数。

for (char c : str.toCharArray())
{
    if (!Character.isDigit(c)) return false;
}
return true;
4

3 回答 3

1

“仅在例外情况下使用例外”是一个普遍遵循的好习惯,但这不是一个硬性规定。我认为这是使用异常比替代方法更好的情况之一。

由于parseInteger()可以返回任何可能的int值,因此不能使用任何其他返回值来指示失败。如果您知道您永远不会处理特定值(例如-1or -2147483648),则可以将其作为标记值返回以指示解析失败。

唯一的选择是返回boolean指示成功或失败并将解析的值存储到参数中。但是,由于函数调用在 Java 中始终是按值传递的,因此您需要创建一个新类来执行此操作:

public class IntWrapper
{
    int value;
}
...

public static boolean myParseInt(String s, IntWrapper outValue)
{
    try
    {
        outValue.value = Integer.parseInt(s);
        return true;
    }
    catch(NumberFormatException e)
    {
        return false;
    }
}
...

IntWrapper value = new IntWrapper();
if (myParseInt(value))
{
    // Use value.value
}
else
{
    // Parsing failed
}

鉴于这些替代方案,我认为最简单的用法就是使用异常并适当地处理它们,即使非数字输入可能不一定是“异常”条件。

于 2013-09-07T20:17:16.940 回答
0

You could use:

public static boolean isInteger(String str) {
    if (str == null) {
        return false;
    }
    int length = str.length();
    if (length == 0) {
        return false;
    }
int i = 0;
if (str.charAt(0) == '-') {
    if (length == 1) {
        return false;
    }
    i = 1;
}
for (; i < length; i++) {
    char c = str.charAt(i);
    if (c <= '/' || c >= ':') {
        return false;
    }
}
return true;
}

Already answered here: What's the best way to check to see if a String represents an integer in Java?

于 2013-12-02T20:21:47.420 回答
0

我会例外,但如果您真的想要解决方案,您可以使用 java 内部类从站点复制方法 parseInt() 并稍微更改一下

(你可以多修改一点,因为你不需要结果)

public static false isValidInt(String s, int radix)
                throws NumberFormatException
    {
        if (s == null) {
            return false;
        }

        if (radix < Character.MIN_RADIX) {
            return false;
        }

        if (radix > Character.MAX_RADIX) {
            return false;
        }

        int result = 0;
        boolean negative = false;
        int i = 0, len = s.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;

        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                } else
                    return false;

                if (len == 1) // Cannot have lone "-"
                    return false;
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++),radix);
                if (digit < 0) {
                    return false;
                }
                if (result < multmin) {
                    return false;
                }
                result *= radix;
                if (result < limit + digit) {
                    return false;
                }
                result -= digit;
            }
        } else {
            return false;
        }
        return true;
    }
于 2013-09-08T01:23:37.483 回答