1

我知道我可以很容易地使用 try/catch 来检查命令行参数是否为双精度。

我正在寻找一种方法来检查这一点,而无需使用昂贵的 try/catch 块。我对Java相当陌生。我搜索了几个论坛,但没有找到任何特定于我的问题的内容。

System.out.println(Double.parseDouble(args[1]));

^^这将导致错误。除了把它放在 try/catch 中之外,有人能指出我是否可以通过数据验证来防止这种情况发生吗?假设 args[1] 是一个文本字符串。

4

4 回答 4

5

try-方法是执行此catch操作的普遍接受的方法,主要是因为实际上有效的双精度可以有很多格式:

double a = 42.e10;  // or 42.E10
double b = 42.f;    // or 42.F
double c = 42.d;    // or 42.D
double d = 010E010D;
double e = 0x1.fffffffffffffP+1023;
double f = 0x1.0p-1022;

所有这些都是有效的双精度,并且可以通过parseDouble(). Double.parseDouble("NaN")andDouble.parseDouble("Infinity")也是有效的(尽管NaNandInfinity不是文字)。更不用说parseDouble()还处理前导或尾随空格。所以我的回答是:不要!使用try-catch方法。可以构造一个匹配某些(或什至全部)有效双格式子集的正则表达式,但我怀疑这实际上会比捕获和处理NumberFormatException.


完整的正则表达式实际上在以下文档中进行了解释valueOf()

为了避免在无效字符串上调用此方法并NumberFormatException抛出异常,可以使用下面的正则表达式来筛选输入字符串:

  final String Digits     = "(\\p{Digit}+)";
  final String HexDigits  = "(\\p{XDigit}+)";
  // an exponent is 'e' or 'E' followed by an optionally
  // signed decimal integer.
  final String Exp        = "[eE][+-]?"+Digits;
  final String fpRegex    =
      ("[\\x00-\\x20]*"+  // Optional leading "whitespace"
       "[+-]?(" + // Optional sign character
       "NaN|" +           // "NaN" string
       "Infinity|" +      // "Infinity" string

       // A decimal floating-point string representing a finite positive
       // number without a leading sign has at most five basic pieces:
       // Digits . Digits ExponentPart FloatTypeSuffix
       //
       // Since this method allows integer-only strings as input
       // in addition to strings of floating-point literals, the
       // two sub-patterns below are simplifications of the grammar
       // productions from section 3.10.2 of
       // The Java™ Language Specification.

       // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
       "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+

       // . Digits ExponentPart_opt FloatTypeSuffix_opt
       "(\\.("+Digits+")("+Exp+")?)|"+

       // Hexadecimal strings
       "((" +
        // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
        "(0[xX]" + HexDigits + "(\\.)?)|" +

        // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
        "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +

        ")[pP][+-]?" + Digits + "))" +
       "[fFdD]?))" +
       "[\\x00-\\x20]*");// Optional trailing "whitespace"

  if (Pattern.matches(fpRegex, myString))
      Double.valueOf(myString); // Will not throw NumberFormatException
  else {
      // Perform suitable alternative action
  }

如您所见,这有点令人讨厌的正则表达式。

于 2013-09-23T15:09:07.427 回答
1

正如评论者 Óscar López 所说,只需使用 try/catch 块并捕获 NumberFormatException。这是确保目标字符串是正确格式的双精度的正确方法,并且 try/catch 构造的潜在开销值得程序的正确性和清晰性。

Double d = null;
try {
  d = Double.parseDouble(args[1]);
} catch (NumberFormatException nfe) {
  // TODO
}
于 2013-09-23T15:09:48.790 回答
1

Java 没有类似isDoubleisNumeric开箱即用的方法,但您可以自己编写它们:

public static boolean isDouble(String s) {
    try {
        Double.parseDouble(s);
    } catch (NumberFormatException e) {
        return false;
    }
    return true;
}

但是double有很多写法:

  • 23.2
  • 23f
  • 23e10
  • 2_3.4_5(不能用 解析Double#parseDouble(String)
  • 2_3E1_0d(不能用 解析Double#parseDouble(String)

如果您只想检查 XX.XXX 模式,这是使用 RegEx 的方法:

public static boolean isDoubleDigits(String s) {
    return s.matches("\\d+(\\.\\d{1,2})?");
}
于 2013-09-23T15:10:05.283 回答
0

为什么不使用命令行输入验证框架?签出Apache Commons CLI 项目

于 2013-09-23T15:26:28.907 回答