0

I am writing this program and do not understand why the loop will not exit and output the System.out.print(). Could someone take a look at it and advise me of whats going on?

public class SalesTax 
{

    public static void main(String[] args) 
    {   
        // Input items for shopping cart
        HashMap<String, Double> cart = new HashMap<String, Double>();

        // Create a Scanner
        Scanner input = new Scanner(System.in).useLocale(Locale.US);

        //variables
        String item;
        double price;
        boolean done = false;
        String ans;
        do
        {       
            System.out.print("Enter the item then select enter followed by the price and enter.");
            item = input.nextLine();
            price = input.nextDouble();
            cart.put(item, price);
            System.out.print("If done type done if not continue with adding to cart.");
            ans = input.nextLine();

            if(ans.equals("Done"))
                done = true;
            else
                item = input.nextLine();
                price = input.nextDouble();
                cart.put(item, price);
                System.out.print("If done type done if not continue with adding to cart.");
        } while( !done);

        System.out.print("Yo");
    }
}
4
  • What IDE are you using? The easiest way to hunt down logic errors is to use the debugger and step through the program. Commented Aug 11, 2014 at 17:19
  • 6
    You need curly braces around the bit after "else". As it is, only the item assignment is done in the else case; the rest is done every time through the loop. Commented Aug 11, 2014 at 17:19
  • Are you sure the loop isn't exiting? Since you're missing curly braces as others have said, the program could be printing the message even when done is true and the loop actually exits. One other thing to check: your message says to type done but the program actually checks for Done. Probably use equalsIgnoreCase instead of equals. Commented Aug 11, 2014 at 17:28
  • I changed it from the user inputing done to input f and the output "yo" still does not come up Commented Aug 11, 2014 at 18:01

3 Answers 3

4

The problem is missing curly braces around the else block: although you have indented the block correctly, the indentation does not matter: only the first line

item = input.nextLine();

is considered part of else; the remaining ones are executed unconditionally.

Note that there is only one place where you set done to true, you could replace the do/while loop with a "forever" loop, and use break to exit the loop in the middle:

while (true) {
    ...
    if (ans.equals("Done")) {
        break;
    }
    ...
}

This makes declaring done variable unnecessary.

Sign up to request clarification or add additional context in comments.

2 Comments

I dont understand. I want the condition of done to become true when the user says it is true.
@pgray10 There is no condition for done in this case, which means that the only exit from the loop is in executing a break statement in the middle. This is a common idiom for situations when the decision on whether to continue or to exit the loop needs to be done in the middle of the loop.
1

In addition to the missing curly braces, it's a problem that your program displays the instructions to type done, but the program actually checks for Done. The equals method requires that the letter case be exactly the same. So if you're following the instructions, it may appear that your loop never exits.

This is one way to fix that problem:

if(ans.equalsIgnoreCase("Done"))

Comments

0
public static void main(String[] args) 
{   
    //Input items for shopping cart
    HashMap<String, Double> cart = new HashMap<String, Double>();

//Create a Scanner
Scanner input = new Scanner(System.in).useLocale(Locale.US);

//variables
String item = "";
double price;
boolean done = false;
String ans = ""; //Initialize to empty, just my common practice.
do
{       
    System.out.print("Enter the item then select enter followed by the price and enter.");
     item = input.nextLine();
     price = input.nextDouble();
    cart.put(item, price);
    System.out.print("If done type done if not continue with adding to cart.");
    ans = input.nextLine();

    if(ans.toLowerCase.equals("done")) //to handle upper and lower case
    {
        done = true;
    }
    else
    { //Add curly braces*************
        item = input.nextLine();
        price = input.nextDouble();
        cart.put(item, price);
        System.out.print("If done type done if not continue with adding to cart.");
    }
}while(!done);

System.out.print("Yo");

}

Essentially thats all you have to do is close your braces. Also, you can make it more modular by moving your first set of .next----() above your do statement and then just having your else statement handle the .next---()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.