A few if in a switch-case block

  • A+
Category:Languages

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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: