【问题标题】:Convert String Type to Double without Double.parseDouble() [duplicate]在没有 Double.parseDouble() 的情况下将字符串类型转换为 Double [重复]
【发布时间】:2015-03-12 11:34:20
【问题描述】:

Double.parseDouble 导致我的程序抛出一些 NullPointerExceptions。有什么方法可以在不使用 .parseDouble 方法的情况下将 String 转换为 double 类型?

我知道String.format() 可以用于Double to string,但是反过来有类似的吗?

编辑:

               try {
                    if(amountEntered != null || amountEntered == ""){
                       amntEntered = Double.parseDouble(amountEntered);
                    }
                    else{
                    //Do nothing   
                    textArea.setText("");
                    System.out.print("");
                    }
                } 
                  catch (NumberFormatException e) {

                    textArea.setText("");
                    System.out.print("");

                }

NullPointerException 堆栈跟踪仍在打印。

【问题讨论】:

  • 您要转换的字符串是什么?
  • 你能分享一下 amountEntered 的值吗
  • @Fahim amntEntered = Double.parseDouble(amountEntered); --- amntEntered 是一个双精度值,amountEntered 是一个字符串。当输入一个空字符串时它会抱怨
  • @Fahim amountEntered = JOptionPane.showInputDialog(finishPayInput, "请输入总额:£" + convPrice); amntEntered = Double.parseDouble(amountEntered);
  • 如果您的 parseDouble 给出 NullPointerException,那么这是因为您向它发送了一个空字符串。对输入字符串进行简单的空检查可以解决此问题。

标签: java string nullpointerexception type-conversion double


【解决方案1】:

试试这个。在将其转换为双重之前应用空检查。

String s="asdf";
    if(s!=null)
     Double d=Double.valueOf(s);

【讨论】:

    【解决方案2】:

    首先,检查值是否不为空。然后,如果您不想捕获 NumberFormatExpression,请考虑使用正则表达式。以下是从http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#valueOf(java.lang.String)复制的解决方案:

    为避免在无效字符串上调用此方法并引发 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
    }
    

    如果性能不重要,可能 try...catch 是一个更好的解决方案。

    【讨论】:

      猜你喜欢
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多