2016-10-22 6 views
0

電卓用のコードをいくつか用意しています(これまでの作業は覚えていますが)。Android - 動作しなければならないコードのエラー

私はarrayList.get()をすべてチェックして、正しいインデックスを指していることを確認しました。ループあたりの正確な数はarrayList.remove()です。

私が考えることができる唯一の他のものは、結果が小数であるかどうかをチェックするループ(おそらく、メソッドの最下部に近いところ)が物事を乱しているということです。ここで

はコードです:

public void onClickEquals (View view) { 

    TextView textViewError = (TextView) findViewById(R.id.textViewCalcPrevRes); 
    TextView textViewCalcPrevRes = (TextView) findViewById(R.id.textViewCalcPrevRes); 
    TextView textViewCalcCurrExp = (TextView) findViewById(R.id.textViewCalcCurrExp); 
    TextView textViewCalcPrevExp = (TextView) findViewById(R.id.textViewCalcPrevExp); 
    double calc = 0; 
    String calcOutputStr = ""; 
    String tempString = ""; 
    int c = arrayList.size(); 

    try { 
     textViewError.setText(""); 
     //i.e. array [2,+,3,*,4,-,3] size(c) = 7, so [2,+,3,*,4,-,3] 
     while (c != 1) { 
      if (c > 3) { 
       if (arrayList.get(3).contains("×") || arrayList.get(3).contains("÷")) { 
        if (arrayList.get(3).contains("×")) {calc = Double.parseDouble(arrayList.get(2)) * Double.parseDouble(arrayList.get(4));} 
        if (arrayList.get(3).contains("÷")) {calc = Double.parseDouble(arrayList.get(2))/Double.parseDouble(arrayList.get(4));} 

        //calc = 12 ;array = [2,+,3,*,4,-,3] 
        arrayList.remove(2); //[2,+,*,4,-,3] 
        arrayList.remove(2); //[2,+,4,-,3] 
        arrayList.remove(2); //[2,+,-,3] 
        arrayList.add(2, Double.toString(calc)); //[2,+,12,-,3] 
        c = arrayList.size(); // size(c) = 5 
       } else { 
        //[2,+,12,-,3] 
        if (arrayList.get(1).contains("+")) {calc = Double.parseDouble(arrayList.get(0)) + Double.parseDouble(arrayList.get(2));} 
        if (arrayList.get(1).contains("-")) {calc = Double.parseDouble(arrayList.get(0)) - Double.parseDouble(arrayList.get(2));} 
        if (arrayList.get(1).contains("×")) {calc = Double.parseDouble(arrayList.get(0)) * Double.parseDouble(arrayList.get(2));} 
        if (arrayList.get(1).contains("÷")) {calc = Double.parseDouble(arrayList.get(0))/Double.parseDouble(arrayList.get(2));} 
        //calc = 14 
        arrayList.remove(0); //[+,12,-,3] 
        arrayList.remove(0); //[12,-,3] 
        arrayList.remove(0); //[-,3] 
        arrayList.add(0, Double.toString(calc)); //[14,-,3] 
        c = arrayList.size(); // size(c) = 3 
       } 
      } 
      // size(c) <= 3 
      else { 
       if (arrayList.get(1).contains("+")) {calc = Double.parseDouble(arrayList.get(0)) + Double.parseDouble(arrayList.get(2));} 
       if (arrayList.get(1).contains("-")) {calc = Double.parseDouble(arrayList.get(0)) - Double.parseDouble(arrayList.get(2));} 
       if (arrayList.get(1).contains("×")) {calc = Double.parseDouble(arrayList.get(0)) * Double.parseDouble(arrayList.get(2));} 
       if (arrayList.get(1).contains("÷")) {calc = Double.parseDouble(arrayList.get(0))/Double.parseDouble(arrayList.get(2));} 
       //calc = 11 
       arrayList.remove(0); //[-,3] 
       arrayList.remove(0); //[3] 
       arrayList.remove(0); //[null] 
       arrayList.add(0, Double.toString(calc)); // [9] 
       c = arrayList.size(); // size(c) = 1 
       prevCalc = Double.toString(calc); 
      } 
      //CHECK IF DECIMAL - SHOULD BE OUTSIDE WHILE LOOP 
      //check if calc is a whole number; if yes, convert to string and enter into tempString, remove decimal and enter into calcOutputStr ready for display on screen. 
      if (calc % 1 == 0) { 
       tempString = Double.toString(calc); 
       if (tempString != null) { 
        tempString = tempString.substring(0, tempString.length() - 2); 
       } 
       calcOutputStr = tempString; 
       arrayList.clear(); 
      } 
      //if calc is a decimal convert to string ready for display on screen. 
      else { 
       calcOutputStr = Double.toString(calc); 
       arrayList.clear(); 
      } 
     } 
      textViewCalcPrevExp.setText(textViewCalcCurrExp.getText()); //copy text from textViewCalcCurrExp to textViewCalcPrevExp 
      textViewCalcCurrExp.setText(""); //remove text from textViewCalcCurrExp 
      textViewCalcPrevRes.setText(calcOutputStr); //display calc 
      stringInput = ""; 
      stringInputWithOp=""; 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     textViewCalcPrevExp.setText(textViewCalcCurrExp.getText()); 
     textViewCalcCurrExp.setText(""); 
     stringInput=""; 
     stringInputWithOp=""; 
     arrayList.clear(); 
     textViewError.setText("ERROR"); 
    } 
} 

私は私の問題を説明するのに役立つ2つのシナリオあげる:、この方法を実行している時にtextViewCalcPrevResディスプレイ4

  • arrayList = [2,+,2,+,2];

    1. arrayList = [2,+,2];をこの方法を実行すると、textViewCalcPrevResが表示されますERROR、例外をスローし、スタックトレースをコンソールに出力するメソッドがあるためです(次のコードブロックを参照)。本当に奇妙なことは、catch{}ステートメントがをtextViewErrorに出力し、textViewCalcPrevResでないことです。

    はここで、シナリオ2のコンソールに出力されたスタックトレースです:

    W/System.err: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 
    W/System.err:  at java.util.ArrayList.get(ArrayList.java:411) 
    W/System.err:  at com.st1.u3141294.sparkscientificcalculator.sparkMain$override.onClickEquals(sparkMain.java:126) 
    W/System.err:  at com.st1.u3141294.sparkscientificcalculator.sparkMain$override.access$dispatch(sparkMain.java) 
    W/System.err:  at com.st1.u3141294.sparkscientificcalculator.sparkMain.onClickEquals(sparkMain.java:0) 
    W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
    W/System.err:  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
    W/System.err:  at android.view.View.performClick(View.java:5610) 
    W/System.err:  at android.view.View$PerformClick.run(View.java:22260) 
    W/System.err:  at android.os.Handler.handleCallback(Handler.java:751) 
    W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
    W/System.err:  at android.os.Looper.loop(Looper.java:154) 
    W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:6077) 
    W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
    W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
    W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
    

    明らかにそれがIndexOutOfBounds例外を投げ、しかし、私は実際にどのように/なぜわかりません。

    どのようなアイデアをお願いしますか?

    EDIT:さらに "調査" の例外の起源は(//-->印)のコード行からであることを示唆している:

    // size(c) <= 3 
          else { 
          //-->if (arrayList.get(1).contains("+")) {calc = Double.parseDouble(arrayList.get(0)) + Double.parseDouble(arrayList.get(2));} 
           if (arrayList.get(1).contains("-")) {calc = Double.parseDouble(arrayList.get(0)) - Double.parseDouble(arrayList.get(2));} 
           if (arrayList.get(1).contains("×")) {calc = Double.parseDouble(arrayList.get(0)) * Double.parseDouble(arrayList.get(2));} 
           if (arrayList.get(1).contains("÷")) {calc = Double.parseDouble(arrayList.get(0))/Double.parseDouble(arrayList.get(2));} 
    

    EDIT:コード-見習い@:

    シナリオ2をarrayList(つまり[2,+,2,+,2])の内容として使用すると、私のコードがうまくいくはずです。コード実行の最初のブロックはc = arrayList.size() = 5ので

    、それはelseループに行くので、何の乗算/除算演算子は、arrayListではありません:トレース以下のc = arrayList.size() = 3以来、

    while (c != 1) {  
        if (c > 3) { 
        //here is code only run if there are multiplication or division operators in arrayList 
        } else { 
         if (arrayList.get(1).contains("+")) {calc = Double.parseDouble(arrayList.get(0)) + Double.parseDouble(arrayList.get(2));} 
        } 
         arrayList.remove(0); //bringing arrayList down to [+,2,+,2] 
         arrayList.remove(0); //then [2,+,2] 
         arrayList.remove(0); //then [+,2] 
         arrayList.add(0, Double.toString(calc)); //then adding 4 (calc) to arrayList[0] would give [4,+,2] 
         c = arrayList.size(); // therefore arrayList.size() = 3 
    } 
    

  • +0

    。 – RThomP

    +0

    "Index:1、Size:0"これはリストが空であることを意味します。デバッグを使用して、これがなぜそうであるかを理解する必要があります。 –

    +0

    さらに助けが必要な場合は、クラッシュの原因となった行を教えてください。 –

    答えて

    2

    単純に、ur arrayList sizeを何度もログに記録してください。あなたはその空がいつわかるでしょう。

    Log.d("Buggy List Size ", arrayList.size()+""); 
    
    +0

    これを実装してコードをデバッグする方法を詳しく説明できますか?私はデバッギング初心者です。 – RThomP

    +0

    私はそれを理解し、コードを修正する手助けをしました!ありがとうございました。 – RThomP

    +0

    また、デバッグ手法の学習を開始してください。それは長期的にあなたを助けるでしょう。 – Sarasranglt

    0

    とすぐに最初の計算が行われていたとして、それが出力に結果を試みることを意味し、CHECK IF DECIMALループはwhileループの中にあった、私は新人のミスを犯したが判明。 2+2+2を計算する代わりに、2+2を計算した後にarrayListをクリアすると、IndexOutOfBounds例外が発生します。

    ここで(それがあるべきであるように、whileループ外部CHECK IF DECIMALループを有する)固定コードであるバンプするコメント

    public void onClickEquals (View view) { 
    
        TextView textViewError = (TextView) findViewById(R.id.textViewCalcPrevRes); 
        TextView textViewCalcPrevRes = (TextView) findViewById(R.id.textViewCalcPrevRes); 
        TextView textViewCalcCurrExp = (TextView) findViewById(R.id.textViewCalcCurrExp); 
        TextView textViewCalcPrevExp = (TextView) findViewById(R.id.textViewCalcPrevExp); 
        double calc = 0; 
        String calcOutputStr = ""; 
        String tempString = ""; 
        int c = arrayList.size(); 
    
        try { 
         textViewError.setText(""); 
         //i.e. array [2,+,3,*,4,-,3] size(c) = 7, so [2,+,3,*,4,-,3] 
         while (c != 1) { 
          if (c > 3) { 
           if (arrayList.get(3).contains("×") || arrayList.get(3).contains("÷")) { 
            if (arrayList.get(3).contains("×")) {calc = Double.parseDouble(arrayList.get(2)) * Double.parseDouble(arrayList.get(4));} 
            if (arrayList.get(3).contains("÷")) {calc = Double.parseDouble(arrayList.get(2))/Double.parseDouble(arrayList.get(4));} 
    
            //calc = 12 ;array = [2,+,3,*,4,-,3] 
            Log.d("BuggyListSizeB4Remove1", arrayList.size()+""); 
            arrayList.remove(2); //[2,+,*,4,-,3] 
            Log.d("BuggyListSizeB4Remove2", arrayList.size()+""); 
            arrayList.remove(2); //[2,+,4,-,3] 
            Log.d("BuggyListSizeB4Remove3", arrayList.size()+""); 
            arrayList.remove(2); //[2,+,-,3] 
            Log.d("BuggyListSizeAfter3", arrayList.size()+""); 
            arrayList.add(2, Double.toString(calc)); //[2,+,12,-,3] 
            c = arrayList.size(); // size(c) = 5 
            Log.d("BuggyListSize AfterCalc", arrayList.size()+""); 
           } else { 
            //[2,+,12,-,3] 
            if (arrayList.get(1).contains("+")) {calc = Double.parseDouble(arrayList.get(0)) + Double.parseDouble(arrayList.get(2));} 
            if (arrayList.get(1).contains("-")) {calc = Double.parseDouble(arrayList.get(0)) - Double.parseDouble(arrayList.get(2));} 
            if (arrayList.get(1).contains("×")) {calc = Double.parseDouble(arrayList.get(0)) * Double.parseDouble(arrayList.get(2));} 
            if (arrayList.get(1).contains("÷")) {calc = Double.parseDouble(arrayList.get(0))/Double.parseDouble(arrayList.get(2));} 
            //calc = 14 
            Log.d("BuggyListSizeB4Remove1", arrayList.size()+""); 
            arrayList.remove(0); //[+,12,-,3] 
            Log.d("BuggyListSizeB4Remove2", arrayList.size()+""); 
            arrayList.remove(0); //[12,-,3] 
            Log.d("BuggyListSizeB4Remove3", arrayList.size()+""); 
            arrayList.remove(0); //[-,3] 
            Log.d("BuggyListSizeAfter3", arrayList.size()+""); 
            arrayList.add(0, Double.toString(calc)); //[14,-,3] 
            c = arrayList.size(); // size(c) = 3 
            Log.d("BuggyListSize AfterCalc", arrayList.size()+""); 
           } 
          } 
          // size(c) <= 3 
          else { 
           if (arrayList.get(1).contains("+")) {calc = Double.parseDouble(arrayList.get(0)) + Double.parseDouble(arrayList.get(2));} 
           if (arrayList.get(1).contains("-")) {calc = Double.parseDouble(arrayList.get(0)) - Double.parseDouble(arrayList.get(2));} 
           if (arrayList.get(1).contains("×")) {calc = Double.parseDouble(arrayList.get(0)) * Double.parseDouble(arrayList.get(2));} 
           if (arrayList.get(1).contains("÷")) {calc = Double.parseDouble(arrayList.get(0))/Double.parseDouble(arrayList.get(2));} 
           //calc = 11 
           Log.d("BuggyListSizeB4Remove1", arrayList.size()+""); 
           arrayList.remove(0); //[-,3] 
           Log.d("BuggyListSizeB4Remove2", arrayList.size()+""); 
           arrayList.remove(0); //[3] 
           Log.d("BuggyListSizeB4Remove3", arrayList.size()+""); 
           arrayList.remove(0); //[null] 
           Log.d("BuggyListSizeAfter3", arrayList.size()+""); 
           arrayList.add(0, Double.toString(calc)); // [9] 
           c = arrayList.size(); // size(c) = 1 
           prevCalc = Double.toString(calc); 
           Log.d("BuggyListSize AfterCalc", arrayList.size()+""); 
          } 
         } 
         //CHECK IF DECIMAL 
         //check if calc is a whole number; if yes, convert to string and enter into tempString, remove decimal and enter into calcOutputStr ready for display on screen. 
         if (calc % 1 == 0) { 
          tempString = Double.toString(calc); 
          if (tempString != null) { 
           tempString = tempString.substring(0, tempString.length() - 2); 
          } 
          calcOutputStr = tempString; 
          arrayList.clear(); 
         } 
         //if calc is a decimal convert to string ready for display on screen. 
         else { 
          calcOutputStr = Double.toString(calc); 
          arrayList.clear(); 
         } 
         //output to textViews 
          textViewCalcPrevExp.setText(textViewCalcCurrExp.getText()); //copy text from textViewCalcCurrExp to textViewCalcPrevExp 
          textViewCalcCurrExp.setText(""); //remove text from textViewCalcCurrExp 
          textViewCalcPrevRes.setText(calcOutputStr); //display calc 
          stringInput = ""; 
          stringInputWithOp=""; 
        } 
        catch (Exception e) { 
         e.printStackTrace(); 
         textViewCalcPrevExp.setText(textViewCalcCurrExp.getText()); 
         textViewCalcCurrExp.setText(""); 
         stringInput=""; 
         stringInputWithOp=""; 
         arrayList.clear(); 
         textViewError.setText("ERROR"); 
        } 
    } 
    
    関連する問題