2016-07-07 40 views
0

它基本上是一個計算器應用程序。當用戶返回編輯他們之前編輯的編輯文本時,會出現該問題,並第二次單擊計算按鈕。我知道答案在我的臉上顯得很渺茫,但我對這個知之甚少,所以我無法找到解決方案。我被困了2周。感謝您提前給予的幫助,我知道這是一個可怕的問題,但我真的很困難。更換代碼非常感謝,因爲我是新手!

的.java代碼 -

package washingtondeli.groupboxlunchesestimateandordering; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.TextView; 

import java.text.NumberFormat; 

public class CapitolhillActivity extends AppCompatActivity { 

    Button mButton; 

    EditText sand1; 
    EditText sand2; 
    EditText sand3; 
    EditText sand4; 
    EditText sand5; 
    EditText sand6; 
    EditText sand7; 

    EditText extra1; 
    EditText extra2; 
    EditText extra3; 
    EditText extra4; 
    EditText extra5; 
    EditText extra6; 


    TextView result1; 
    TextView result2; 
    TextView result3; 
    TextView result4; 
    TextView result5; 
    TextView result6; 
    TextView result7; 

    TextView extraresult1; 
    TextView extraresult2; 
    TextView extraresult3; 
    TextView extraresult4; 
    TextView extraresult5; 
    TextView extraresult6; 

    CheckBox delivery; 
    TextView subtotal; 
    TextView total; 
    int d; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_capitolhill); 

     sand1= (EditText)findViewById(R.id.sand1); 
     sand2= (EditText)findViewById(R.id.sand2); 
     sand3= (EditText)findViewById(R.id.sand3); 
     sand4= (EditText)findViewById(R.id.sand4); 
     sand5= (EditText)findViewById(R.id.sand5); 
     sand6= (EditText)findViewById(R.id.sand6); 
     sand7= (EditText)findViewById(R.id.sand7); 

     extra1= (EditText)findViewById(R.id.extra1); 
     extra2= (EditText)findViewById(R.id.extra2); 
     extra3= (EditText)findViewById(R.id.extra3); 
     extra4= (EditText)findViewById(R.id.extra4); 
     extra5= (EditText)findViewById(R.id.extra5); 
     extra6= (EditText)findViewById(R.id.extra6); 

     result1= (TextView)findViewById(R.id.result1); 
     result2= (TextView)findViewById(R.id.result2); 
     result3= (TextView)findViewById(R.id.result3); 
     result4= (TextView)findViewById(R.id.result4); 
     result5= (TextView)findViewById(R.id.result5); 
     result6= (TextView)findViewById(R.id.result6); 
     result7= (TextView)findViewById(R.id.result7); 

     extraresult1= (TextView)findViewById(R.id.extraresult1); 
     extraresult2= (TextView)findViewById(R.id.extraresult2); 
     extraresult3= (TextView)findViewById(R.id.extraresult3); 
     extraresult4= (TextView)findViewById(R.id.extraresult4); 
     extraresult5= (TextView)findViewById(R.id.extraresult5); 
     extraresult6= (TextView)findViewById(R.id.extraresult6); 

     subtotal= (TextView) findViewById(R.id.subtotal); 
     total= (TextView) findViewById(R.id.total); 
     final CheckBox delivery= (CheckBox) findViewById(R.id.checkBox); 

     delivery.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (delivery.isChecked()) { 
        d = 25; 
       } else { 
        d = 0; 
       } 
      } 
     }); 

     mButton = (Button)findViewById(R.id.calc); 
     mButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (delivery.isChecked()) { 
        d = 25; 
       } else { 
        d = 0; 
       } 

       calculate2(); 
      } 
     }); 
    } 

    public void calculate2() { 
     //get entered texts from the edittexts,and convert to integers. 
     Integer value1 = Integer.parseInt(sand1.getText().toString()); 
     Integer value2 = Integer.parseInt(sand2.getText().toString()); 
     Integer value3 = Integer.parseInt(sand3.getText().toString()); 
     Integer value4 = Integer.parseInt(sand4.getText().toString()); 
     Integer value5 = Integer.parseInt(sand5.getText().toString()); 
     Integer value6 = Integer.parseInt(sand6.getText().toString()); 
     Integer value7 = Integer.parseInt(sand7.getText().toString()); 

     Integer value8 = Integer.parseInt(extra1.getText().toString()); 
     Integer value9 = Integer.parseInt(extra2.getText().toString()); 
     Integer value10 = Integer.parseInt(extra3.getText().toString()); 
     Integer value11 = Integer.parseInt(extra4.getText().toString()); 
     Integer value12 = Integer.parseInt(extra5.getText().toString()); 
     Integer value13 = Integer.parseInt(extra6.getText().toString()); 

     Integer valuesubtotal = Integer.parseInt(subtotal.getText().toString()); 
     Integer valuetotal = Integer.parseInt(total.getText().toString()); 
     //do the calculation 

     Double calculatedValue1 = (9.5*value1); 
     Double calculatedValue2 = (9.5*value2); 
     Double calculatedValue3 = (9.5*value3); 
     Double calculatedValue4 = (9.5*value4); 
     Double calculatedValue5 = (9.5*value5); 
     Double calculatedValue6 = (9.5*value6); 
     Double calculatedValue7 = (9.5*value7); 

     Double calculatedValue8 = (2.25*value8); 
     Double calculatedValue9=(1.5*value9) ; 
     Double calculatedValue10=(3.5*value10) ; 
     Double calculatedValue11 =(3.95*value11) ; 
     Double calculatedValue12 = (3.5*value12); 
     Double calculatedValue13 = (2.95*value13); 

     Double calculatedsubtotal = (d+calculatedValue1+calculatedValue2+calculatedValue3+calculatedValue4+calculatedValue5+calculatedValue6+calculatedValue7+calculatedValue8+calculatedValue9+calculatedValue10+calculatedValue11+calculatedValue12+calculatedValue13); 
     Double calculatedtotal = ((.1*calculatedsubtotal)+calculatedsubtotal); 

     //set the value to the textview, to display on screen. 
     result1.setText("$"+String.format("%.2f", calculatedValue1)); 
     result2.setText("$"+ String.format("%.2f", calculatedValue2)); 
     result3.setText("$"+ String.format("%.2f", calculatedValue3)); 
     result4.setText("$"+ String.format("%.2f", calculatedValue4)); 
     result5.setText("$"+ String.format("%.2f", calculatedValue5)); 
     result6.setText("$"+ String.format("%.2f", calculatedValue6)); 
     result7.setText("$"+ String.format("%.2f", calculatedValue7)); 

     extraresult1.setText("$"+ String.format("%.2f", calculatedValue8)); 
     extraresult2.setText("$"+ String.format("%.2f", calculatedValue9)); 
     extraresult3.setText("$"+ String.format("%.2f", calculatedValue10)); 
     extraresult4.setText("$"+ String.format("%.2f", calculatedValue11)); 
     extraresult5.setText("$"+ String.format("%.2f", calculatedValue12)); 
     extraresult6.setText("$"+ String.format("%.2f", calculatedValue13)); 

     subtotal.setText("$"+ String.format("%.2f", calculatedsubtotal)); 
     total.setText("$"+ String.format("%.2f", calculatedtotal)); 
    } 
} 
+2

請與您的問題發表您的異常堆棧跟蹤。看起來你沒有正確解析用戶的輸入。 –

+0

也許你真的想使用NumberFormat貨幣實例。 –

回答

1

調用之前(的TextView/EDITTEXT).getText()。toString()方法,刪除$符號並使用double而不是int。

試試這個:

String total = "$10.5"; 
    String strAmount = total.replace("$",""); 
    double value = Double.valueOf(strAmount); 
    Log.e("VALUE", String.valueOf(value)); 

例子:

public void calculate2() { 
    //get entered texts from the edittexts,and convert to integers. 
    Double value1 = moneyToDouble(sand1.getText().toString()); 
    Double value2 = moneyToDouble(sand2.getText().toString()); 
    Double value3 = moneyToDouble(sand3.getText().toString()); 
    Double value4 = moneyToDouble(sand4.getText().toString()); 
    Double value5 = moneyToDouble(sand5.getText().toString()); 
    Double value6 = moneyToDouble(sand6.getText().toString()); 
    Double value7 = moneyToDouble(sand7.getText().toString()); 

    Double value8 = moneyToDouble(extra1.getText().toString()); 
    Double value9 = moneyToDouble(extra2.getText().toString()); 
    Double value10 = moneyToDouble(extra3.getText().toString()); 
    Double value11 = moneyToDouble(extra4.getText().toString()); 
    Double value12 = moneyToDouble(extra5.getText().toString()); 
    Double value13 = moneyToDouble(extra6.getText().toString()); 

    Double valuesubtotal = moneyToDouble(subtotal.getText().toString()); 
    Double valuetotal = moneyToDouble(total.getText().toString()); 
    //do the calculation 

    Double calculatedValue1 = (9.5*value1); 
    Double calculatedValue2 = (9.5*value2); 
    Double calculatedValue3 = (9.5*value3); 
    Double calculatedValue4 = (9.5*value4); 
    Double calculatedValue5 = (9.5*value5); 
    Double calculatedValue6 = (9.5*value6); 
    Double calculatedValue7 = (9.5*value7); 

    Double calculatedValue8 = (2.25*value8); 
    Double calculatedValue9=(1.5*value9) ; 
    Double calculatedValue10=(3.5*value10) ; 
    Double calculatedValue11 =(3.95*value11) ; 
    Double calculatedValue12 = (3.5*value12); 
    Double calculatedValue13 = (2.95*value13); 

    Double calculatedsubtotal = (d+calculatedValue1+calculatedValue2+calculatedValue3+calculatedValue4+calculatedValue5+calculatedValue6+calculatedValue7+calculatedValue8+calculatedValue9+calculatedValue10+calculatedValue11+calculatedValue12+calculatedValue13); 
    Double calculatedtotal = ((.1*calculatedsubtotal)+calculatedsubtotal); 

    //set the value to the textview, to display on screen. 
    result1.setText("$"+String.format("%.2f", calculatedValue1)); 
    result2.setText("$"+ String.format("%.2f", calculatedValue2)); 
    result3.setText("$"+ String.format("%.2f", calculatedValue3)); 
    result4.setText("$"+ String.format("%.2f", calculatedValue4)); 
    result5.setText("$"+ String.format("%.2f", calculatedValue5)); 
    result6.setText("$"+ String.format("%.2f", calculatedValue6)); 
    result7.setText("$"+ String.format("%.2f", calculatedValue7)); 

    extraresult1.setText("$"+ String.format("%.2f", calculatedValue8)); 
    extraresult2.setText("$"+ String.format("%.2f", calculatedValue9)); 
    extraresult3.setText("$"+ String.format("%.2f", calculatedValue10)); 
    extraresult4.setText("$"+ String.format("%.2f", calculatedValue11)); 
    extraresult5.setText("$"+ String.format("%.2f", calculatedValue12)); 
    extraresult6.setText("$"+ String.format("%.2f", calculatedValue13)); 

    subtotal.setText("$"+ String.format("%.2f", calculatedsubtotal)); 
    total.setText("$"+ String.format("%.2f", calculatedtotal)); 
} 

private Double moneyToDouble(String total){ 

    String strAmount = total.replace("$",""); 
    double value = Double.valueOf(strAmount); 

    return value; 
} 
+0

我真的不好,在這裏,你介意編輯我的代碼,因爲我真的迷路了。我知道只是要求某人爲我做這件事只是因爲它已經過了2周和3個問題,並且我無法弄清楚 – Casey

+0

因此,我是否將此複製並粘貼到我的計算2中?我必須爲每個價值做底部部分,但對嗎? – Casey

+0

是的,複製並粘貼calculate2方法和moneyToDouble方法,告訴我你是否還有其他問題。 –

1

刪除您的$符號。這是由於它不是一個數字而引起的問題。還要將您的變量類型改爲兩倍。

+2

Eh,[我不會那麼快地改變變量類型](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency)。 – rmlan

+0

我真的很糟糕,你會介意編輯我的代碼,因爲我真的迷路了。我知道只是要求某人爲我做這件事很不好,因爲它已經過了2周和3個問題,而且我無法弄清楚 – Casey

+0

您是否解決了這個問題? – Jas

0

它正在第二次崩潰,因爲$被添加到您的編輯文本值,並且當您在該edittext上執行getString時,它會給您帶有$符號的字符串。你當你爲一個值計算做第二次

顯示您刪除$符號根據你的代碼

String value=subtotal.getText().toString(); 
String replace=value.replace("$",""); 
Integer valuesubtotal = Integer.parseInt(replace); 
+0

我真的很糟糕,你會介意編輯我的代碼,因爲我真的迷路了。我知道這是不好的,只是要求別人爲我做這只是它已經2周和3個問題後,我無法弄清楚 – Casey

+0

@Casey更新我的答案根據您的代碼 –

+0

感謝虐待現在嘗試它 – Casey

相關問題