Warm tip: This article is reproduced from serverfault.com, please click

How can I create an if statement if there are no values for the textfield to ignore the method?

发布于 2020-11-30 07:08:02

I would like to print only the following items if there are values in the TextField but i can't find an answer to it. Previously I used Checkbox to var.isSelected() method to toggle those buttons but it seems like it seems like the design and functions of it are not much an impact and I think I can remove it and apply a different method to the function.

If there are no values in the textfield, the program should ignore the statement and move to the other.

Sorry for my english.

My code:

    try{dishCarbonara = Double.parseDouble(dishCarbonaraTF.getText());}
        catch(NumberFormatException e){dishCarbonaraTF.setText("0");}
    dishes += dishCarbonara * 35 ;
    listItems += "Carbonara: 35 * "+dishCarbonara+"\n";
    
    try{dishChickenWing = Double.parseDouble(dshChickenWIngsTF.getText());}
        catch(NumberFormatException e){dshChickenWIngsTF.setText("0");} 
    dishes += 50 * dishChickenWing;
    listItems += "Chicken Wings: 30 * "+dishChickenWing+"\n";
    
    try{dishPotatoFries = Double.parseDouble(dishPotatoFriesTF.getText());}
        catch(NumberFormatException e){dishPotatoFriesTF.setText("0");} 
    dishes += 25 * dishPotatoFries;
    listItems += "Potato Fries: 25 * "+dishPotatoFries+"\n";
Questioner
AKWARD
Viewed
0
David Kroukamp 2020-11-30 15:47:44

How about a simple if check?

if (!dishCarbonaraTF.getText().equals("")) {
    // text field is not empty do something with the value 
}

You would do this for each TextField of course.

You could also do the below to ensure there aren't just whitespaces being entered:

if (!dishCarbonaraTF.getText().trim().equals("")) {
    // textfield is not empty and doesn't contain any white spaces
}