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";
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
}
your answer is good and it helped me but the output didn't change so i think of the try and catch statements to insert it inside the if statement and it worked! anyways thanks for the help :)