android - 从 String 数组类型转换为 Double 时出现 NumberFormatException

标签 android numberformatexception

我正在尝试将字符串转换为 double 。我可以在很多情况下做到这一点,但这次,它给了我一个 NumberFormatException。这是代码...

//strExpression = 2^3 (My Input in the editText)

strExpression = edtxtExpression.getText().toString();

try
{
   System.out.println("Inside Power case");
   //expSplit is a String[]
   expSplit = strExpression.split("^");
   double first = Double.parseDouble(expSplit[0]);
   System.out.println("First Number "+first);
   double second = Double.parseDouble(expSplit[1]);
   System.out.println("Second Number "+second);

   double result = Math.pow(first, second);
   System.out.println("Result "+result);
   edtxtExpression.setText(first+" ^ "+second+" = "+result);

}
catch (Exception e) {
    // TODO: handle exception
    Toast.makeText(this, e.getClass().toString(), 100).show();

}

异常:::

01-01 03:07:45.169: I/System.out(11287): Inside Power case
01-01 03:07:45.179: I/System.out(11287): class java.lang.NumberFormatException

我得到“Inside Power”输出,但其他打印语句没有执行。我哪里错了?

最佳答案

^是RegEx中的保留字符,试试\\^

expSplit = strExpression.split("\\^");

关于android - 从 String 数组类型转换为 Double 时出现 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14106275/

相关文章:

android - 将应用程序的 Beta 版本发布为生产版本

java - 使用 JSON 和 php 将 DB Mysql 与 android 应用程序连接起来

java - 尝试使用parseInt()和trim()时,NumberFormatException无法修复它

java - java.lang.NumberFormatException 问题

安卓 keystore : Debug/Release key for each app?

java - 解析 AsyncTask 时出错

android - 如何检测用户何时打开/关闭 GPS 状态?

java - 发送过多消息时服务器/客户端错误

java - 单击按钮后我的应用程序崩溃

java - Float.parseFloat 返回没有意义的 NumberFormatException