Domanda

I'm trying to build a tip calc. This is part of my code:

public void onTextChanged(CharSequence s, int start, int before, int count) {

    double billAmount = Double.parseDouble(s.toString());
    tip10EditText.setText("" + billAmount * .1);
    tip15EditText.setText("" + billAmount * .15);
    tip20EditText.setText("" + billAmount * .2);
    total10EditText.setText("" + (billAmount + billAmount * 0.1));
    total15EditText.setText("" + (billAmount + billAmount * 0.15));
    total20EditText.setText("" + (billAmount + billAmount * 0.2));

}

The problem is, when I delete everything from the bill edit text, the app crashes.. My friend found some way to deal with it. Here is he's code:

public void onTextChanged(CharSequence s, int start, int before, int count) {

    if (!s.toString().equals("")) {
        bill = Double.parseDouble(s.toString());

        tip10EditText.setText((bill / 10) + "");
        total10EditText.setText((bill + (bill / 10)) + "");

        // the String.format("%.2f", is to SHORTEN the tail of the double to 2 digits
        tip15EditText.setText(String.format("%.2f", (bill / 6.6666) ));
        total15EditText.setText(String.format("%.2f",(bill + (bill / 6.6666)) ));
        tip20EditText.setText(String.format("%.2f", (bill / 5) ));
        total20EditText.setText(String.format("%.2f",(bill + (bill / 5)) ));

        // to handle the seekBar changing according to the bill
        double billCu = Double.parseDouble(billEditText.getText().toString());
        // total20EditText.setText(String.format("%.2f",(tipOf ));
        double progressPer=Double.parseDouble(customTipTextView.getText().toString());
        double tipOf = (billCu/100)*progressPer;
        tipCustomEditText.setText(String.format("%.2f",(tipOf )));
        totalCustomEditText.setText(String.format("%.2f",((billCu+tipOf) )));
    }
    else {
        tip10EditText.setText( "");
        total10EditText.setText( "");
        tip15EditText.setText( "");
        total15EditText.setText( "");
        tip20EditText.setText( "");
        total20EditText.setText( "");
        tipCustomEditText.setText("");
        totalCustomEditText.setText("");            
    }
}

Is there a better way to deal with this problem?

È stato utile?

Soluzione

public void onTextChanged(CharSequence s, int start, int before, int count) {
   double billAmount = 0.0;
   if(s.length() != 0){
        billAmount = Double.parseDouble(s.toString());
   }
        tip10EditText.setText("" + billAmount * .1);
        tip15EditText.setText("" + billAmount * .15);
        tip20EditText.setText("" + billAmount * .2);
        total10EditText.setText("" + (billAmount + billAmount * 0.1));
        total15EditText.setText("" + (billAmount + billAmount * 0.15));
        total20EditText.setText("" + (billAmount + billAmount * 0.2));
}

The above code calculate proper value only if any input present in textbox otherwise display 0.

Altri suggerimenti

try this :

     if (!TextUtils.isEmpty(s)) {

     Then do your stuff....

}

put your code in following if statement :

   if (s.length() > 0)

use try catch block with NumberFormatException in catch block and by using this u can handle wrong type of input also.

        try{
            //your code
        }catch(NumberFormatException npe){
            //handle the exception and intimate the user if needed.
        }catch(Exception e){

        }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top