Afif Amin
Afif Amin

Reputation: 1

calculator error when click without Value

I need help, I build the calculator application, but when I click the add, sub or equal button without value, the application will crash.Otherwise, if I insert the value first is running without error. help

Button for Equal

FloatingActionButton fab = findViewById(R.id.fab);
fab.bringToFront();
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Value2 = Float.parseFloat(ed1.getText() + "" );
        if (mAddition == true){
            ed1.setText(Value1 + Value2 +"");
            mAddition=false;
        }
        if (mSubtract == true){
            ed1.setText(Value1 - Value2 +"");
            mSubtract=false;
        }
    }
}); 

Button for Sub

FloatingActionButton btn_Sub = findViewById(R.id.btn_Sub);
btn_Sub.bringToFront();
btn_Sub.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Value1 = Float.parseFloat(ed1.getText() + "");
        mSubtract = true ;
        ed1.setText("");
    }
});

Button for add

FloatingActionButton btn_Add = findViewById(R.id.btn_Add);
btn_Add.bringToFront();
btn_Add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (ed1 == null){
            ed1.setText("");
        }else {
            Value1 = Float.parseFloat(ed1.getText() + "");
            mAddition = true;
            ed1.setText("");
        }
    }
});

what should i do?

This is my logcat

 04-29 22:27:18.861 10722-10722/afif.calfits V/FA: onActivityCreated
04-29 22:27:18.891 10722-10722/afif.calfits D/skia_ext: turboImageDecoder took 27.87ms to decode 1024x768 (1x downscale)
04-29 22:27:19.151 10722-10744/afif.calfits V/FA: Activity resumed, time: 5774739
04-29 22:27:19.151 10722-10744/afif.calfits D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=Food, firebase_previous_id(_pi)=-2803431669891477858, firebase_screen_class(_sc)=Dairy, firebase_screen_id(_si)=-8225789602144573450}]
04-29 22:27:20.261 10722-10722/afif.calfits D/AndroidRuntime: Shutting down VM
04-29 22:27:20.261 10722-10722/afif.calfits E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: afif.calfits, PID: 10722
                                                              java.lang.NumberFormatException: Invalid float: ""
                                                                  at java.lang.StringToReal.invalidReal(StringToReal.java:63)
                                                                  at java.lang.StringToReal.parseFloat(StringToReal.java:308)
                                                                  at java.lang.Float.parseFloat(Float.java:306)
                                                                  at afif.calfits.Dairy$4.onClick(Dairy.java:95)
                                                                  at android.view.View.performClick(View.java:4756)
                                                                  at android.view.View$PerformClick.run(View.java:19761)
                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5253)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Upvotes: 0

Views: 93

Answers (3)

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

It is because you're trying to parsing an empty string which is not a correct parameter for Float.parseFloat() method. From the following documentation:

public static float parseFloat(String s) throws NumberFormatException

Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.

Parameters:
s - the string to be parsed.
Returns:
the float value represented by the string argument.
Throws:
NullPointerException - if the string is null
NumberFormatException - if the string does not contain a parsable float.

You can see that the method will throw NullPointerException if the string is null or NumberFormatException if string not a float. An empty string is not a float, so you will receive the NumberFormatException

So, you need to check for empty string and trap the error with something like this:

String text = ed1.getText().toString();
float value;

// check if text is empty or not.
if(text.isEmpty()) {
  value = 0;
} else {
  try {
    value = Float.parseFloat(text);    
  } catch(NumberFormatException e) {
    // text is not a number, set it as 0.
    value = 0;
  }
}

Upvotes: 1

Janvi Vyas
Janvi Vyas

Reputation: 752

You are not adding log or error so I can't get exact your error or problem but after showing your code I think, the problem in this line:-

here, You need to add .toString() with editext.gettext().

In,Button for Equal replace this line

Value2 = Float.parseFloat(ed1.getText() + "" );

with

 if( !ed1.getText().toString().equals("")){

            Value2 = Float.parseFloat(ed1.getText().toString() + "" );
    }

Same as in Button for Sub

FloatingActionButton btn_Sub = findViewById(R.id.btn_Sub);
btn_Sub.bringToFront();
btn_Sub.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

 if( !ed1.getText().toString().equals("")){

        Value1 = Float.parseFloat(ed1.getText().toString() + "");
        mSubtract = true ;
        ed1.setText("");
    }
 }
});

Same as in Button for add

FloatingActionButton btn_Add = findViewById(R.id.btn_Add);
btn_Add.bringToFront();
btn_Add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (ed1 == null){
            ed1.setText("");
        }else {
          if( !ed1.getText().toString().equals("")){   
            Value1 = Float.parseFloat(ed1.getText().toString() + "");
            mAddition = true;
            ed1.setText("");

           }
        }
    }
});

Upvotes: 0

Md Sufi Khan
Md Sufi Khan

Reputation: 1761

I think your problem is not related to .toString () rather than NumberFormatException.

Float.parseFloat() throws NumberFormatException − if the string does not contain a parsable float.

So when you put nothing in your edittext it gets nothing but empty string. And empty string is not a parsable number and you got NumberFormatException.

For more derails click here

You can do like following:

Button for Sub

FloatingActionButton btn_Sub = findViewById(R.id.btn_Sub);
btn_Sub.bringToFront();
btn_Sub.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
            Value1 = Float.parseFloat(ed1.getText() + "");
            mSubtract = true ;
            ed1.setText("");
        } catch(NumberFormatException e) {
            Toast.makeText(YOUR_ACTIVITY_CLASS.this, "Invalid Number", Toast.LENGTH_LONG).show();
        }
    }
});

Upvotes: 0

Related Questions