DataGuy
DataGuy

Reputation: 1725

Breaking a while loop by case in Java

I need to issue break a while loop if a case is matched in a switch statement. The case is as follows: if a user enters either a number less than zero or anything greater than five. I have the switch working most of my cost except the two exceptions Ive mentioned. Here is my code:

import java.util.Scanner;
public class Product
{
    public static void main(String args[])
    {
        int cntr = 0;
        int product = 0;
        int units = 0;
        double totalcost = 0;
        Scanner MK = new Scanner(System.in);
        cntr=0;
        while (cntr >= 0 && cntr <=5)
        {
            System.out.println("Enter Product no.(1-5) or -1 to Quit");
            product = MK.nextInt();
            switch(product) {

            case 1:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                product = MK.nextInt();
                double cost = 2.98;
                totalcost = totalcost + cost*product;
                System.out.println("Current total cost: " + totalcost);
                cntr++;
            }
            break;
            case 2:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                product = MK.nextInt();
                double cost = 4.50;
                totalcost = totalcost + cost*product;
                System.out.println("Current total cost: " + totalcost);
                cntr++;
            }
            break;
            case 3:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                product = MK.nextInt();
                double cost = 9.98;
                totalcost = totalcost + cost*product;
                System.out.println("Current total cost: " + totalcost);
                cntr++;
            }
            break;
            case 4:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                product = MK.nextInt();
                double cost = 4.49;
                totalcost = totalcost + cost*product;
                System.out.println("Current total cost: " + totalcost);
                cntr++;
            }
            break;
            case 5:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                product = MK.nextInt();
                double cost = 6.87;
                totalcost = totalcost + (cost*product);
                System.out.println("Current total cost: " + totalcost);
                cntr++;
            }
            case 6:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                //product = MK.nextInt();
                //double cost = 2.98;
                //totalcost = totalcost + cost*product;
                System.out.println("Current total cost: " + totalcost);
                //cntr++;
            }
            break;
            }
            System.out.println("Total cost-->" +totalcost);
        }
    }
}

If anyone can point me in the right direction on how to stop my program when anything less than zero or greater than 5 is entered I would really appreciate it.

Upvotes: 0

Views: 71

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074495

A couple of options:

  1. Use a flag that you set when you want the loop to end (in your default or -1 clause), or

  2. Use a labelled statement and a directed break in your default (or -1) clause

  3. In this specific situation where you said you want the entire program to end, you could use System.exit

Here's more about option 2:

label: while (...) {          // <== Labelled statement
    switch (...) {
        case ...:
            // ...
            break;            // <=== Normal (undirected) break, just exits what
                              //      it's in (switch in this case)
        // ...
        default:
            break label;      // <=== Directed break, exits loop
    }
}

It's also handy for nested loops.


Side note: There's no need for the { and } you have surrounding the statements in your case clauses. The code for case continues until the break. (Yes, it's a bit different from other statements.)


Side note 2: You've said Enter Product no.(1-5) or -1 to Quit in your prompt, but you have cases for the values 1 to 6 (inclusive). You probably meant Enter Product no.(1-**6**) or -1 to Quit.

Upvotes: 4

Related Questions