我正在尝试将字符串转换为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”输出,但其他打印语句没有执行。我哪里错了?
答案 0 :(得分:1)
^
是RegEx中的保留字符,请尝试\\^
。
expSplit = strExpression.split("\\^");