-3

So, I'm trying to make a simple menu with switches.

I have a letter choice inside it. I'm using next().charAt(0); to scan a letter.

It worked well, but I want to simplify it. you see, I have to make a case each choice both uppercase and lowercase.

So how to ignore case so I don't have to make both cases?

Also note: I'm using the old version of Java and Netbeans 8.2 as my IDE. (because my college keeps insisting not to use the newer one. Probably because they don't have the textbook yet.), so probably newer syntax wouldn't work.

my code:

import java.util.Scanner;

public class NewClass2 {


      public static void main(String[] args) {


      Scanner input = new Scanner (System.in);
      char milk;
       int s, h, price;
       h = 0;
       price = 0;
       String type, size;
      System.out.println("NutMart");
      System.out.println("Milk selections:\n A. Milk A \n\t 1. Regular ($10) \n\t 2. Medium ($20) \n\t 3. Large ($30)");
      System.out.println(" B. Milk B \n\t 1. Regular ($15) \n\t 2. Medium ($30) \n\t 3. Large ($45)");
       System.out.println(" C. Milk C \n\t 1. Regular ($20) \n\t 2. Medium ($40) \n\t 3. Large ($60)");

      System.out.println("Insert Milk Type: ");
      milk = input.next().charAt(0);

      switch(milk){
              case 'a':
              type = "Milk A";
              h = 10;
              break;
              case 'b':
              type = "Milk B";
              h = 15;
              break;
              case 'c':
              type = "Milk C";
              h = 20;
              break;
              case 'A':
             type = "Milk A";
              h = 10;
              break;
              case 'B':
              type = "Milk B";
              h = 15;
              break;
              case 'C':
              type = "Milk C";
              h = 20;
              break;
              default: 
                  System.out.println("Invalid!");
                  System.out.println("Please select the correct choice: ");
      milk = input.next().charAt(0);
                     break;
      }
      System.out.println("Select the size: ");
    while (!input .hasNextInt()) input .next();
    s = input.nextInt();

      switch(s){
          case 1:
              size = "Regular";
              price = h * 1;
              break;
              case 2:
              size = "Medium";
              price = h * 2;
              break;
              case 3:
              size = "Large";
              price = h * 3;
              break;
               default: 
                  System.out.println("Invalid");
                   System.out.println("Please select the correct choice: ");
    while (!input .hasNextInt()) input .next();
    s = input.nextInt();
                     break;
      }
          System.out.println("Individual Price: $" + price);
        System.out.println("Please insert the quantity: ");
    while (!input .hasNextInt()) input .next();
    int quantity = input.nextInt();

    int total = price * quantity;


      System.out.println("Your total will be $" + total );

      }
}

1 Answers1

2

Note, that in this case converting to a consistent case is the best was to achieve your goal. But when you have a variety of results for dissimilar inputs in a switch statement you can do the following for case constructs.

      case 'a':
      case 'A:
              type = "Milk A";
              h = 10;
              break;
      ...

The case will simply fall thru whether 'a' or 'A' was provided.

WJS
  • 36,363
  • 4
  • 24
  • 39
  • Ah! The same thing came to my mind, OP should know this way also with the case converting hack. – Eklavya Oct 22 '20 at 15:33