Search code examples
javajoptionpane

do while loop exiting in jOptionPane


Consider the following code. For some reason, even after hitting the NO button, it's exiting the do while loop. I recently introduced the recentPurchaseAmount variable and it stopped working after that. Even removing that is not making it work now.

Variables like purchaseAmount , recentPurchaseAmount are double type initialized to 0.0. itemNo, itemCheck are integer type variables.

do {
    purchaseAmount = Double.parseDouble(JOptionPane.showInputDialog(
        "Enter the Item Purchase Amount"));
    recentPurchaseAmount = recentPurchaseAmount + purchaseAmount;

    if (onepty.budgetAmountVerify(recentPurchaseAmount)) {
        itemNo++;
        itemCheck = JOptionPane.showConfirmDialog(null,
            "Finished Purchasing Items Purchase Amount?",
            "Say Yes or No", JOptionPane.YES_NO_OPTION); 

        remainingBal = onepty.remainingBalance(recentPurchaseAmount);        
    }
}   while(itemCheck == JOptionPane.YES_OPTION);

JOptionPane.showMessageDialog(null, 
    "Total items purchased are " + itemNo + 
    "\n and the remaining balance is : " +remainingBal+"" ,
    "This is the Title",JOptionPane.INFORMATION_MESSAGE);

Solution

  • while(itemCheck == JOptionPane.NO_OPTION);will be a more suitable condition.