简体   繁体   中英

A few if in a switch-case block

I have a question as to how I can perform several checks inside the switch-case? I need to do a couple of checks in case 2, but adding a second if block, my application does nothing, it just hangs. What was I wrong about?

BufferedReader inputCommand = new BufferedReader(new InputStreamReader(System.in));

        while (true) {
                    System.out.println("Instruction:");
                    System.out.println();
                    System.out.println("1 -- Show all product at the store");
                    System.out.println("2 -- Add the product at the client basket");
                    System.out.println("3 -- Show client basket");
                    System.out.println();
                    switch (inputCommand.readLine()) {
                        case "1":
                            basketCommand.get();
                            System.out.println();
                            break;
                        case "2":
                            System.out.println();
                            System.out.println("Select product to add into your basket");
                            if (inputCommand.readLine().equals("su")){
                                basketCommand.addIntoBasket(productContainer.productList.get("su"));
                            }
                            if (inputCommand.readLine().equals("an")){
                                basketCommand.addIntoBasket(productContainer.productList.get("an"));
                            }
                            break;
    }

In the second case statement, you should read the next input only once:

BufferedReader inputCommand = new BufferedReader(new InputStreamReader(System.in));

while (true) {
    System.out.println("Instruction:");
    System.out.println();
    System.out.println("1 -- Show all product at the store");
    System.out.println("2 -- Add the product at the client basket");
    System.out.println("3 -- Show client basket");
    System.out.println();
    switch (inputCommand.readLine()) {
    case "1":
        basketCommand.get();
        System.out.println();
        break;
    case "2":
        System.out.println();
        System.out.println("Select product to add into your basket");

        String next = inputCommand.readLine();
        if (next.equals("su")) {
            basketCommand.addIntoBasket(productContainer.productList.get("su"));
        }
        else if (next.equals("an")) {
            basketCommand.addIntoBasket(productContainer.productList.get("an"));
        }
        break;
    }
}

Your first if statement is swallowing the input line, so the second if statement has nothing to read.

Just store the line after reading it once :

                String readLine = inputCommand.readLine();

                if (readLine.equals("su")){
                    basketCommand.addIntoBasket(productContainer.productList.get("su"));
                }
                else if (readLine.equals("an")){
                    basketCommand.addIntoBasket(productContainer.productList.get("an"));
                }

Everytime you do a inputCommand.readLine() the Programm waits for an input on your side. You can't compare a single input like you did in this example. The best way would be to save the input in a variable and then perform your checks. Something like this would work (not tested):

BufferedReader inputCommand = new BufferedReader(new InputStreamReader(System.in));
    while (true) {
                System.out.println("Instruction:");
                System.out.println();
                System.out.println("1 -- Show all product at the store");
                System.out.println("2 -- Add the product at the client basket");
                System.out.println("3 -- Show client basket");
                System.out.println();
                switch (inputCommand.readLine()) {
                    case "1":
                        basketCommand.get();
                        System.out.println();
                        break;
                    case "2":
                        System.out.println();
                        System.out.println("Select product to add into your basket");
                        String input = inputCommand.readLine();
                        if (input.equals("su")){
                            basketCommand.addIntoBasket(productContainer.productList.get("su"));
                        }
                        if (input.equals("an")){
                            basketCommand.addIntoBasket(productContainer.productList.get("an"));
                        }
                        break;
}

Replace if with else if

case "2":
     System.out.println();
     System.out.println("Select product to add into your basket");
     if (inputCommand.readLine().equals("su")){
        basketCommand.addIntoBasket(productContainer.productList.get("su"));
     }
     else if (inputCommand.readLine().equals("an")){
     basketCommand.addIntoBasket(productContainer.productList.get("an"));
     } 
     break;

I assumed this is a basic program for learning purposes so i tried to keep it simple.

1.Don't forget to close Scanner class.

2.always convert digit input to LowerCase(consider case of 'q' vs 'Q')

3.everytime you execute "scanner.nextLine()" your program will halt and wait for input from source(in that case System.in configured to take input from keyboard)

4.System.out.println will print '\\n' to the screen at the end of its input causing it to create a new line, you can use this char inside your string to save code lines.

Complete Code:

import java.util.Scanner;
import java.io.IOException;

public class MyClass {

public static void ShowMenu()
{
      System.out.println("\nInstruction:");
      System.out.println("1 -- Show all product at the store");
      System.out.println("2 -- Add the product at the client basket");
      System.out.println("3 -- Show client basket");
      System.out.println("q -- Quit Program");


}

public static void ProductMenu()
{
      System.out.println("Select product to add into your basket");
      System.out.println();
      System.out.println("ba -- Basketball");
      System.out.println("fi -- Fiat 500");
      System.out.println("ip -- Iphone");
      System.out.println();
}

public static void Exit(Scanner sc) {
    sc.close();
    System.out.println("You've exited the program. goodbye!");
    System.exit(1);
}

public static void main(String args[]) {
    Scanner scanner = new Scanner(System.in);
    String inputCommand = null;

    while (true) {
       ShowMenu();
        switch (inputCommand = scanner.nextLine().toLowerCase()) {
            case "1":
                System.out.println("you entered 1");
                break;
            case "2":
                ProductMenu();
                inputCommand = scanner.nextLine(); //should block

                if (inputCommand.equals("ba")){
                    System.out.println("Basketball added.");
                }
                if (inputCommand.equals("fi")){
                    System.out.println("Fiat 500 added.");


                }
                if (inputCommand.equals("ip")){
                    System.out.println("Iphone added.");

                }
                break;

            case "q":
                Exit(scanner);
                break;
            default:
                System.out.println("Invalid Input");
                break;
        }
   }

}

}

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