0

在这个用于计算行列式的应用程序中,我使用了一个 java 解析器形式: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)
4

2 回答 2

0

将无效字符串转换为数字时会发生Generall NumberFormatException

例如: matrix1[i][j]="21.21"那没关系

但如果 matrix1[i][j]="a21.21" <--它会给你NumberFormatException

在你的情况下vettore1[i][j] = Double.parseDouble(matrix1[i][j]);

尝试通过打印值来检查您的响应字符串使用LOGSystem.out.println(matrix1[i][j]); 测试您的所有响应值

于 2012-06-07T18:24:23.557 回答
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 
 }
于 2012-06-07T18:25:05.483 回答