简体   繁体   中英

Check if editText is empty or not

I have an editText (called input) where the user enters a value and then chooses a radio button to convert that value to a different unit of measure. I am trying to set up an if statement so that it displays a text message if a radio button isn't chosen and the edittext is empty but it keeps crashing the program!

public void startCalculation() {
    RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup);
    int selectedConversion = rg.getCheckedRadioButtonId();
    double input = Double.parseDouble(((EditText) findViewById(R.id.currentSpeed)).getText().toString());
    double speed = 0;
    String measurement = "";
    System.out.println("The Value of Input is: " + input);

    //If its 0 then no unit has been selected
    if(selectedConversion != -1 && input > 0) { 

any idea whats causing the program to crash?

Edit: Here's the log:

10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: FATAL EXCEPTION: main
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: Process: com.example.dan14.windproject, PID: 2058
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime: java.lang.NumberFormatException: Invalid double: ""
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at java.lang.StringToReal.parseDouble(StringToReal.java:267)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at java.lang.Double.parseDouble(Double.java:301)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at com.example.dan14.windproject.MainActivity.startCalculation(MainActivity.java:88)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at com.example.dan14.windproject.MainActivity$2.onClick(MainActivity.java:49)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at android.view.View.performClick(View.java:5198)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21147)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-27 13:39:02.357 2058-2058/com.example.dan14.windproject E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

problem is this line of code:

double input = Double.parseDouble(((EditText) findViewById(R.id.currentSpeed)).getText().toString());

you pass a empty String and that leads to an NumberFormatException like in your case.

Make it more readable with that code:

public void startCalculation() {
    RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup);
    int selectedConversion = rg.getCheckedRadioButtonId();
    String lInputString = ((EditText) findViewById(R.id.currentSpeed)).getText().toString();
    double input = 0;
    if (!lInputString.trim().equals("")){
      input = Double.parseDouble(lInputString);
    }
    double speed = 0;
    String measurement = "";
    System.out.println("The Value of Input is: " + input);

    //If its 0 then no unit has been selected
    if(selectedConversion != -1 && input > 0) { 

Your String is empty so there is an error when you try to parse it. Check if the String is not empty

String text = ((EditText) findViewById(R.id.currentSpeed)).getText().toString();

if(!text.isEmpty()) {
    double input = Double.parseDouble(text);
}

or catch NumberFormatException

try {
    double input = Double.parseDouble(((EditText) findViewById(R.id.currentSpeed)).getText().toString());
} catch(NumberFormatException e) {
    //Error during parse
}
public void startCalculation() {
RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup);
int selectedConversion = rg.getCheckedRadioButtonId();
try{
double input = Double.parseDouble(((EditText) findViewById(R.id.currentSpeed)).getText().toString());
}catch(Exception e)
{
//if catch block is execute it means edittext is empty
}
double speed = 0;
String measurement = "";
System.out.println("The Value of Input is: " + input);

//If its 0 then no unit has been selected
if(selectedConversion != -1 && input > 0) { 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM