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

java-如果文本字段中没有可忽略该方法的值,如何创建if语句?

(java - 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

我只想打印以下项目,如果中有值,TextField但是我找不到答案。以前我曾经Checkboxvar.isSelected()法切换这些按钮,但看起来它似乎是设计和它的功能都没有太大的影响,我想我可以将其删除,并应用不同的方法给函数。

如果文本字段中没有值,则程序应忽略该语句,然后移至另一条语句。

对不起我的英语不好。

我的代码:

    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

简单的if检查怎么样?

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

TextField当然会为每个课程进行此操作

你还可以执行以下操作以确保不只输入空格:

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