Reputation: 27
This code is for a button of a basic grocery calculator. When I push the button an input dialog box shows where you enter your item price. The problem I am having is that I cannot figure out how to get a do ... while
loop to make the input dialog pop back up after an entry is made.
I want it to always come back up unless the user selects ok with nothing, or cancel, in which case the loop should break and fill the remaining boxes. With the current code I have to push the button to bring the dialog back up manually each time. I have been playing around with different while conditions and if statements but I cannot seem to get it to work. I am a beginner so sorry if this is simple, and thank you for your time.
NOTE: I left the while condition empty on purpose, just showing thats where I need it.
NumberFormat numForm = NumberFormat.getCurrencyInstance();
double itemPrice;
double tax;
do {
String s = (String)JOptionPane.showInputDialog("Enter item price:");
if (s == null || s.equals("")) {
double subtotal = getPurchase();
double total;
tax = subtotal * .065;
txtTax.setText(numForm.format(tax));
total = tax + subtotal;
txtTotal.setText(numForm.format(total));
} else {
try {
itemPrice = Double.parseDouble (s);
recordPurchase(itemPrice);
txtPrice.setText(numForm.format(itemPrice));
double subtotal = getPurchase();
txtSubtotal.setText(numForm.format(subtotal));
int items = getItems();
String totalItems = Integer.toString(items);
txtItems.setText(totalItems);
} // end try
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "You must enter numeric data only!");
} // end catch
} // end if Else
}// end do
while();
Upvotes: 2
Views: 333
Reputation: 732
do {
String s = (String)JOptionPane.showInputDialog("Enter item price:");
//user has pressed the cancel button then s becomes null
if(s == null){
break;
}
// your code
}
while(true);
Upvotes: 0
Reputation: 25096
This might read nicer as a while
?
String s;
while ((s = (String) JOptionPane.showInputDialog("Enter item price:")) != null) {
if (s.isEmpty()) { break; }
try {
itemPrice = Double.parseDouble (s);
recordPurchase(itemPrice);
txtPrice.setText(numForm.format(itemPrice));
double subtotal = getPurchase();
txtSubtotal.setText(numForm.format(subtotal));
int items = getItems();
String totalItems = Integer.toString(items);
txtItems.setText(totalItems);
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "You must enter numeric data only!");
}
}
double subtotal = getPurchase();
double total;
tax = subtotal * .065;
txtTax.setText(numForm.format(tax));
total = tax + subtotal;
txtTotal.setText(numForm.format(total));
This way the code that executes at the end is shown that way visually.
Upvotes: 1
Reputation: 31194
You put a condition in your while statement. If the condition is true, it will keep iterating.
String s = "";
do
{
s = (String)JOptionPane.showInputDialog("Enter item price:");
if (s == null || s.equals(""))
{
...
}
...
}while(s != null || !s.equals(""));
Upvotes: 2