简体   繁体   中英

What is wrong with my do while loop in Java

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");
    }
}

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.

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"))
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---()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM