在这个用于计算行列式的应用程序中,我使用了一个java解析器formel:http://www.speqmath.com/tutorials/expression_parser_java/index.html,以便允许用户输入输入数学表达式。但该应用程序崩溃
secondo = new Button (this);
secondo.setText("Fill the matrix and CALCULATE");
tableLayout.addView(secondo);
secondo.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
expr[i][j] = valore[i][j].getText().toString();
matrix[i][j] = prs.parse(expr[i][j]);
}
}
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
int index1 = matrix[i][j].indexOf("=");
matrix1[i][j] = matrix[i][j].substring(index1+2);
}
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
vettore1[i][j] = Double.parseDouble(matrix1[i][j]);
}
}
Matrix A = new Matrix(vettore1);
det = A.det();
这段代码给了我这个错误:
06-07 17:59:09.138: E/AndroidRuntime(2420): FATAL EXCEPTION: main
06-07 17:59:09.138: E/AndroidRuntime(2420): java.lang.NumberFormatException:
06-07 17:59:09.138: E/AndroidRuntime(2420): at org.apache.harmony.luni.util.FloatingPointParser.parseDblImpl(Native Method)
06-07 17:59:09.138: E/AndroidRuntime(2420): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:283)
06-07 17:59:09.138: E/AndroidRuntime(2420): at java.lang.Double.parseDouble(Double.java:287)
06-07 17:59:09.138: E/AndroidRuntime(2420): at java.lang.Double.valueOf(Double.java:326)
06-07 17:59:09.138: E/AndroidRuntime(2420): at it.bisemanuDEV.matrixDET.SecondaAttivita$1.onClick(SecondaAttivita.java:143)
06-07 17:59:09.138: E/AndroidRuntime(2420): at android.view.View.performClick(View.java:2408)
06-07 17:59:09.138: E/AndroidRuntime(2420): at android.view.View$PerformClick.run(View.java:8819)
06-07 17:59:09.138: E/AndroidRuntime(2420): at android.os.Handler.handleCallback(Handler.java:603)
06-07 17:59:09.138: E/AndroidRuntime(2420): at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 17:59:09.138: E/AndroidRuntime(2420): at android.os.Looper.loop(Looper.java:123)
06-07 17:59:09.138: E/AndroidRuntime(2420): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-07 17:59:09.138: E/AndroidRuntime(2420): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 17:59:09.138: E/AndroidRuntime(2420): at java.lang.reflect.Method.invoke(Method.java:521)
06-07 17:59:09.138: E/AndroidRuntime(2420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-07 17:59:09.138: E/AndroidRuntime(2420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-07 17:59:09.138: E/AndroidRuntime(2420): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
Generall NumberFormatException
对于ex:matrix1[i][j]="21.21"
然后没关系
但如果matrix1[i][j]="a21.21"
<--
它会为您提供 NumberFormatException
在你的案例中vettore1[i][j] = Double.parseDouble(matrix1[i][j]);
尝试通过打印值检查您的响应字符串
使用LOG
或System.out.println(matrix1[i][j]);
测试所有响应值
答案 1 :(得分:0)
把这行放在try catch中,就像在字符串中有非数字字符的可能性
if(matrix1[i][j]!=null && matrix1[i][j].trim().length()>0)
try{
vettore1[i][j] = Double.parseDouble(matrix1[i][j].trim());
}catch(NumberFormatException nfe){
//Toast here the string matrix1[i][j] to check
}