1

我正在尝试将字符串转换为双精度。我在很多情况下都能做到这一点,但这一次,它给了我一个 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”输出,但其他打印语句没有被执行。我哪里错了?

4

1 回答 1

1

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

expSplit = strExpression.split("\\^");
于 2012-12-31T22:00:06.060 回答