Results 1 to 2 of 2
  1. #1
    AJ2009 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default Repeat while answer yes - do while loop

    Can someone help, it seems like either i type in "y" or "n", the program won't go back to main menu...
    also compile error:
    variable itemPrice might not have been initialized.
    Should i change to while loop instead? I'm new to while and do-while loop.. Please advice.


    import java.text.*; // to use DecimalFormat
    import java.util.*; // to use Scanner class
    import javax.swing.*; // In order to use JOptionPane

    public class ModOrderSystemA
    {
    public static final double TAX_AMOUNT = 0.50;

    public static void main(String[] args)
    {
    String choice, itemName, input, total, answer= "Y";
    int quantity;
    double itemPrice, priceBeforeTax, priceAfterTax;
    boolean anyMore = true;
    Scanner keyboard = new Scanner (System.in);

    //Display the food menu
    do
    {
    choice =
    JOptionPane.showInputDialog("Welcome to the Restruant!\n" +
    "Here is today's special: \n" +
    "A. Rice $1.50\n" +
    "B. Noddle $2.00\n" +
    "C. Soup $2.50\n" +
    "Please enter your order(A,B,C:");

    choice = choice.toUpperCase();

    if (! choice.equals ("A") &&
    ! choice.equals ("B") &&
    ! choice.equals ("C"))

    {
    JOptionPane.showMessageDialog (null,
    "We don't have choice " + choice + " in our menu.\n" +
    "The order has to be A,B,C,D,E,F or G");
    }
    //else if(choice.equals ("A") &&
    // choice.equals ("B") &&
    // choice.equals ("C"))
    else
    {
    input =
    JOptionPane.showInputDialog("How many would you like to order:");

    quantity = Integer.parseInt(input);

    if (quantity <= 0)
    {
    JOptionPane.showMessageDialog (null,
    "Error: Invalid quantity.\n");
    }
    else
    {
    // Display name of the Food
    // Set price of the item
    if (choice.equals ("A"))
    {
    itemName = " Rice";
    itemPrice = 1.50;
    }
    else if (choice.equals ("B"))
    {
    itemName = " Noddle";
    itemPrice = 2.00;
    }
    else
    {
    itemName = " Soup";
    itemPrice = 2.50;
    }

    JOptionPane.showMessageDialog (null,
    " You ordered " + quantity + itemName);

    answer = JOptionPane.showInputDialog("Would you like another order?\n" +
    "Please enter Y or y for another order.\n" +
    "Enter N or n to exit");

    answer = keyboard.next ();
    }
    }
    if(!answer.equalsIgnoreCase("y"))
    {
    anyMore = false;
    priceBeforeTax = itemPrice * quantity;
    priceAfterTax = priceBeforeTax + TAX_AMOUNT;

    DecimalFormat oneAfter = new DecimalFormat ("0.00");

    JOptionPane.showMessageDialog(null,
    "Here is the total price for your orders: \n" +
    "Price before tax is $" + oneAfter.format(priceBeforeTax) +
    "\nTax is $" + TAX_AMOUNT +
    "\nPrice after tax is $" + oneAfter.format(priceAfterTax) +
    "\n\nThank you for your order and See you again!");
    }
    }while(answer.equalsIgnoreCase("y"));
    }
    }
    Last edited by AJ2009; 01-10-2009 at 03:14 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What are you doing in this lone of code.

    Java Code:
    answer = keyboard.next ();
    Once you get an input from the user, VM machine wait to see another input on command line. Until you enter the value the rest of the code wont execute. Remove that line and see.

    And also please use code tags when you posting again in the forum. Unformated codes are really hard to read.

Similar Threads

  1. Why is the answer not coming out
    By anonymous18 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 04:10 AM
  2. Replies: 3
    Last Post: 11-02-2008, 02:29 AM
  3. Plz answer this question ...
    By raghu2114 in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 07:36 PM
  4. Replies: 1
    Last Post: 07-02-2008, 03:15 PM
  5. nested <ui:repeat> - problem
    By hackerofcrackers in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 06-20-2008, 11:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •