Search code examples
javaandroidfloating-pointnumberformatexception

Cannot parse a String into Float in my app


At first I am new in android development. I tried to make this small app where I wanted to take inputs from the users and show them to a (default)list view. When I put values in all the fields it works fine except when I keep the CGPA field empty it stopped. Unfortunately, MyApp is stopped

It works fine when I filled all the values

Here is the OnClickListener login button.


        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setData();
            }
        });

Here is the lines of setData() method where i guess the problem has occurred. My question is "Is it a right approach to parse data?"

Float cgpa = Float.parseFloat(editTextCgpa.getText().toString());
 if(cgpa<=4.00 && cgpa > 0.00 ){
                student.setCgpa(cgpa);
            }
            else{
                error = true;
                editTextCgpa.setError("CGPA must be within 4 in scale");
            }

Android Monitor:

Here is the error message that i found in android monitor.

11-25 13:06:18.917 7896-7896/com.ariful.lict.lictpractice E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        java.lang.NumberFormatException: Invalid float: ""
                                                                            at java.lang.StringToReal.invalidReal(StringToReal.java:63)
                                                                            at java.lang.StringToReal.parseFloat(StringToReal.java:289)
                                                                            at java.lang.Float.parseFloat(Float.java:300)
                                                                            at com.ariful.lict.lictpractice.MainActivity.setData(MainActivity.java:55)
                                                                            at com.ariful.lict.lictpractice.MainActivity$1.onClick(MainActivity.java:45)
                                                                            at android.view.View.performClick(View.java:4084)
                                                                            at android.view.View$PerformClick.run(View.java:16966)
                                                                            at android.os.Handler.handleCallback(Handler.java:615)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                            at android.os.Looper.loop(Looper.java:137)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:511)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                            at dalvik.system.NativeStart.main(Native Method)

here does the convertion of the String to Float is causing the error ?? Is there anyone who can help me?


Solution

  • You are passing an empty string to be parsed...

    Just change your code to check for null/empty String.

    if(!editTextCgpa.getText().toString().isEmpty()){
    Float cgpa = Float.parseFloat(editTextCgpa.getText().toString());
     if(cgpa<=4.00 && cgpa > 0.00 ){
                    student.setCgpa(cgpa);
                }
                else{
                    error = true;
                    editTextCgpa.setError("CGPA must be within 4 in scale");
                }
    }