1

文字列を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

「InsidePower」出力が表示されますが、他のprintステートメントが実行されません。どこが間違っているのですか?

4

1 に答える 1

1

^は正規表現で予約されている文字です。試してください\\^

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