1

We were ask to make a program using the switch statement..

Here is my code:

    double price = 0, totalPrice;

    System.out.print("Enter the number of your choice: ");
    int optionNumber = Integer.parseInt(kb.readLine());

    switch(optionNumber)
    {
        case 1:
            price = 190.00;
            break;
        case 2:
            price = 410.00;
            break;
        default:
            System.out.println("ERROR: Invalid number!");
            break;
    }

    System.out.print("Enter quantity: ");
    int quantity = Integer.parseInt(kb.readLine());

    totalPrice = price * quantity;

So basically, the user will input a certain number and it will have different prices... inside the switch statements. but if the user inputs a wrong number, it will display an error message and i dont want the user to enter the quantity which will be executed after the switch statement. we are not allowed to use any methods or functions and i dont want to code repeatedly like this:

    System.out.print("Enter the number of your choice: ");
    int optionNumber = Integer.parseInt(kb.readLine());

    switch(optionNumber)
    {
        case 1:
            price = 190.00;
            System.out.print("Enter quantity: ");
            int quantity = Integer.parseInt(kb.readLine());
            totalPrice = price * quantity;
            System.out.print("Total price: " + totalPrice);
            break;
        case 2:
            price = 410.00;
            System.out.print("Enter quantity: ");
            int quantity = Integer.parseInt(kb.readLine());
            totalPrice = price * quantity;
            System.out.print("Total price: " + totalPrice);
            break;
        default:
            System.out.println("ERROR: Invalid number!");
            break;
    }

is there any other way not to use if else, methods, functions or coding repeatedly? ANY HELP WILL BE APPRECIATED.

4 Answers4

1

Use as this:

while(!valid option)
   //do this stuff

Use a flag and set it to true if the number entered is valid, so it will go to your next instruction; else ask again for input.

z21
  • 129
  • 6
1

You can use a boolean flag and make it false if invalid option is selected.
Then only ask user further if flag is true.

    System.out.print("Enter the number of your choice: ");
    int optionNumber = Integer.parseInt(kb.readLine());
    boolean flag = true;
    switch (optionNumber) {
        case 1:
            price = 190.00;
            break;
        case 2:
            price = 410.00;
            break;
        default:
            flag = false;
            System.out.println("ERROR: Invalid number!");
            break;
    }
    if (flag) {
        System.out.print("Enter quantity: ");
        int quantity = Integer.parseInt(kb.readLine());
        totalPrice = price * quantity;
        System.out.print("Total price: " + totalPrice);
    }
afzalex
  • 8,598
  • 2
  • 34
  • 61
0

You could just remove default from the switch statement and check to see if the price is equal to 0 after the switch statement

double price = 0, totalPrice;

System.out.print("Enter the number of your choice: ");
int optionNumber = Integer.parseInt(kb.readLine());

switch(optionNumber)
{
    case 1:
        price = 190.00;
        break;
    case 2:
        price = 410.00;
        break;
}

if (price == 0)
{
     System.out.println("ERROR: Invalid number!");
}
else
{
    System.out.print("Enter quantity: ");
    int quantity = Integer.parseInt(kb.readLine());
    totalPrice = price * quantity;
    System.out.print("Total price: " + totalPrice);
}

This keeps you from adding unnecessary variables (like a boolean flag) when you already have one (price) with a default value of 0 that you can check against.

Howard Renollet
  • 4,609
  • 1
  • 24
  • 31
  • Quoting the OP: _is there any other way not to use if else [...]_, `if else` is not an option (don't know why tough) – Narmer Jan 20 '15 at 14:01
  • @Narmer I think he's talking about using `if/else` statements repeatedly, like inside every `case`. That's how I understood the question anyway. I would think a single `if/else` statement would be ok the way I read the question. *"is there any other way not to use if else, methods, functions or coding **repeatedly***? " – Howard Renollet Jan 20 '15 at 14:03
  • I think that _repeatedly_ refers to _coding_ (as in _i dont want to code repeatedly like this_ written above that). This doesn't leave out the fact that the OP isn't completely clear on what she wants to do. – Narmer Jan 20 '15 at 14:08
  • 1
    Well, the accepted answer dissipates all the doubts :D – Narmer Jan 20 '15 at 14:10
0

Throw an exception

  default:
            throw new InvalidArgumentException("Invalid number!");

See also InvalidArgumentException vs UnexpectedValueException

Community
  • 1
  • 1
blank
  • 17,852
  • 20
  • 105
  • 159