简体   繁体   中英

compare an integer to a null value

I need to see if a text field has an empty value. I need to see if

if(Double.parseDouble(distanceTf.getText())==0) 

I know 0 won't work. I also know null won't work and I know .equals won't work. Does anyone know how I can compare this line of code to a null value?

if (stageTf.getText().equals("") || Double.parseDouble(distanceTf.getText()) == null) {
      JOptionPane.showMessageDialog(null, "You did not enter both a stage number and distance");
      return;
}

Thanks for all the above replies but they don't work.

The part of the code I have trouble with is:

if (Double.parseDouble(distanceTf.getText())==null)

The rest of it is fine.

I have tried putting this outside the if statement and using distanceTf.getText().equals("") in the if statement but this doesn't work either.

I just can't find out how to assign an empty value to the line of code for a double.

I know null , .equals or "" won't work.

You're not clear on which value could be null, so I'll assume both.

Since Double.parseDouble requires a non-null argument, you need to check it for null .

if(null != distanceTf.getText() && Double.parseDouble(distanceTf.getText()) != 0.0)

stageTf.getText() could return null too, but if you're guaranteed to be comparing a known non-null String against null , it would return false. So, this comparison is safer:

if("".equals(stageTf.getText())

The important thing to understand is: what you mean with null value? A null reference or an empty string?

You could do

stageTf.getText().isEmpty()

to check if the string is empty and parse it only if it contains something.

// here remember it's still wrong
if (!stageTf.getText().isEmpty() && Double.parseDouble(distanceTf.getText()) == null) {

Second problem: Double.parseDouble doesn't return null since it returns a native type.. it thrown an exception if something went wrong. So you can catch NumberFormatException.

Then you could write:

try { 
 double result;
 if (!stageTf.getText().isEmpty() && (result = Double.parseDouble(distanceTf.getText()))) { 
  /* i think you need the result of the conversion, so i saved it in result */
 }
}
catch (NumberFormatException e) { /* something went wrong! */ }

You need to test if the field is empty first. You did it correctly with your first conditional on the stageTf field. You need to do the same with the distanceTF field. This means nesting your conditional statements.

if(stageTF.getText().equals(""))
    if(distanceTF.getText().equals("")){
        /* ... */
    } else {
        //here it is safe to test for exceptions by using a try/catch
        try{
           //here you can parse the string to your Double
        }catch(NumberFormatException nfe){ /* ... */ }
    }

first of all you should check for null before empty because if the value is null you'll get a NullPointerException on the first one. Second you'll get a NullPointerException if distanceTf.getText() is null on the Double.parseDouble Double.parseDouble() doc

what I would do is create a method validate as follows:

private boolean validate(String field){ //where field = stageIf.getText() for example

   if(field != null && field.trim().length() > 0)
         return true;

   else return false;
}

解析外部语句,然后进行比较:

if(distanceTf.getText() == "")

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