0

The app crash when there is no text ! I don't have what 'if' to put to fix that ! Thanks you

enter  TextView TotalTextView;
EditText EnterPercentage;
EditText EnterPrice;

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


    TotalTextView = (TextView) findViewById(R.id.TotalTextView);
    EnterPercentage = (EditText) findViewById(R.id.EnterPercentage);
    EnterPrice = (EditText) findViewById(R.id.EnterPrice);
    Button CalcBtn = (Button) findViewById(R.id.CalcBtn);


    CalcBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                float percentage = Float.parseFloat(EnterPercentage.getText().toString());
                float prix = Float.parseFloat(EnterPrice.getText().toString());
                float dec = percentage / 100;
                float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());

                TotalTextView.setText(String.format("%.2f", total));


        }
    });

Thank for your answer, I am a begineer so... Thanks !

if (EnterPercentage.getText().equals("")) {

                float percentage = Float.parseFloat(EnterPercentage.getText().toString());
                float prix = Float.parseFloat(EnterPrice.getText().toString());
                float dec = percentage / 100;
                float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());
                TotalTextView.setText(String.format("%.2f", total));


            } else {

}

the button don't do anything but the app doesn't crash

K.Mir
  • 19
  • 6

4 Answers4

5

We check the length of the String... when it's 0 then we do nothing. When the String > 0 your code is running.

Like:

            if (EnterPercentage.getText().trim().toString().length() == 0 || EnterPrice.getText().toString().length() == 0 ) {
                //Textfields are empty.
                Log.d("Error","Fields are empty");
            } else {
                //Textfield is full
                float percentage = Float.parseFloat(EnterPercentage.getText().toString());
                float prix = Float.parseFloat(EnterPrice.getText().toString());
                float dec = percentage / 100;
                float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());
                TotalTextView.setText(String.format("%.2f", total));
            }
  • If I want the app to do anything when the String is empty, what I have to say on "else" ? – K.Mir Oct 21 '16 at 21:45
  • What ever you want. –  Oct 21 '16 at 21:49
  • @Tim, I'm sorry, but this is not how you compare String objects, as indicated by Mike's link. – Pieter van den Ham Oct 22 '16 at 12:33
  • Hey ! I tried but I don't know why I have the problem with your brand new code ! – K.Mir Oct 22 '16 at 17:32
  • Sry. Try it again –  Oct 22 '16 at 17:35
  • Hum.. Actually, the app doesn't crash when the cases are empties but nothing happens when I put number.... I worked before :( – K.Mir Oct 22 '16 at 19:32
  • What do you mean exactly?? –  Oct 22 '16 at 19:48
  • I mean the button doesn't work now, even if I enter numbers in my EditText ! But the app doesn't crash when I don't put anything in my EditText... – K.Mir Oct 22 '16 at 19:57
  • There isn't error message, and the log don't say anything about that... – K.Mir Oct 22 '16 at 20:08
  • if (EnterPercentage.getText().equals("")) { } else { float percentage = Float.parseFloat(EnterPercentage.getText().toString()); float prix = Float.parseFloat(EnterPrice.getText().toString()); float dec = percentage / 100; float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString()); TotalTextView.setText(String.format("%.2f", total)); } But the app crash when I don't put numbers as yesterday – K.Mir Oct 22 '16 at 20:10
  • I understand the Problem now. I post a new answer tomorrow –  Oct 22 '16 at 20:17
  • I changed the code a little bit. But I tested it and it works. When your EnterPrecentage and EnterPrice is empty nothing is done. When both are full your code is running. When only one is full nothing is done. Have fun with the code. When you have problems you can write again :). Maybe you can accept my aswer?? :) –  Oct 23 '16 at 08:25
4

You need to check (obviously) your string. The if that your code is missing is :

if (TextUtils.isEmpty(EnterPercentage.getText()) || TextUtils.isEmpty(EnterPrice.getText())) {
// YOUR CODE
}

Good luck with android development. Questions was already posted

Nawako
  • 342
  • 3
  • 17
1

You surely got a NullPointerException. That's because the text is null and you put a .toString() after.

Try this:

float percenteage = 0;
float prix = 0;

if (EnterPercentage.getText() != null)
    percenteage = Float.parseFloat(EnterPercentage.getText().toString());
if (EnterPrice.getText() != null)
    prix = Float.parseFloat(EnterPercentage.getText().toString());
mhSangar
  • 457
  • 3
  • 12
0

you need to validate before trying to convert to float

float percentage = getFloat();
private float getFloat(){
    float fRet = 0.0f;
    if(your validation here){
        fRet = Float.parseFloat(EnterPercentage.getText().toString());;
    }
    return fRet;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97