2012-06-07 117 views
0

在這個應用程序的行列式的計算,我使用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

這是一個'NumberFormatException'。你確定'matrix1 [i] [j]'包含數字作爲'字符串'而沒有別的? –

+0

想起來,我想你是發佈了一個關於'sin30'和其他這樣的數學函數的問題的人。在嘗試將它轉換爲'Double'之前,你是否已經分析了你的輸入? –

+0

是的,實際上是我用在一個簡單的Java應用程序中的相同代碼,並且工作正常,但是在Android應用程序中給我提供了錯誤 – whiteTIGER

回答

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]); 檢查您的響應字符串來測試所有的響應值

0

放嘗試捕捉這條線作爲字符串有非數字字符的可能性

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 
}