Serhat Kara Serhat Kara - 7 months ago 11
Java Question

java looping using a Yes or No option Pane

my objective is to read from the keyboard a description of an expense and then an actual expense and then ask YES or NO and if YES it loops back to ask for another expense description and expense all whilst adding the number of expenses and the total the user input. I know what to do if the option is no i am just having trouble looping the yes option back to start

heres what i have so far

public static double budgetAmount;
public static int totalExpenses;
public static String expenseDescription;
public static int expense;
public static boolean proceed;
public static String value;
public static double runningTotal;
public static int answer;

public static void main(String[] args) {

String bdgtAmnt1 = JOptionPane.showInputDialog("Enter Budget Amount:");
budgetAmount = Double.parseDouble(bdgtAmnt1);
do {

expenseDescription = JOptionPane.showInputDialog("Enter the description of the Expense in 32 letters");
String expense1 = JOptionPane.showInputDialog("Enter Expense Amount:");
budgetAmount = Double.parseDouble(expense1);
if (answer == JOptionPane.YES_OPTION) runningTotal += expense;
totalExpenses++;

answer = JOptionPane.showConfirmDialog(null, "Are there any more items to be entered?", "End Expense Report", JOptionPane.YES_NO_OPTION);

if (answer == JOptionPane.YES_OPTION)
System.exit(0);
else if (answer == JOptionPane.NO_OPTION)
System.out.println("One more time");

} while(answer == JOptionPane.YES_OPTION);
}

Answer

Your logic looks inverted: if I press yes, then I should keep putting more items in the list... So change this:

if (answer == JOptionPane.YES_OPTION)
    System.exit(0);
else if (answer == JOptionPane.NO_OPTION)
    System.out.println("One more time");

for:

if (answer == JOptionPane.NO_OPTION)
    System.exit(0);
else if (answer == JOptionPane.YES_OPTION)
    System.out.println("One more time");