Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to use logical or ( || ) in switch case?

I need to write a program which takes a user input of any month and returns the number of days in said month. Here is the code that i wrote.

    Scanner keyboard = new Scanner(System.in);
    out.print("Enter month : ");
    String month = keyboard.nextLine().toLowerCase();
    switch (month){
        case "january" :
            out.println("31 days");
            break;

        case "february" :
            out.println("");
            out.print("Is it a leap year (yes/no): ");
            String leap_year = keyboard.nextLine().toLowerCase();
            if (leap_year.equals("no")){
                out.println("28 days");
                break;
            } if (leap_year.equals("yes")){
                out.println("29 days");
                break;  
            } else{
                out.println("Invalid option.. Use yes/no only.");
                break;
            }
            
        case "march" :
            out.println("31 days");
            break;
        case "april" :
            out.println("30 days");
            break;

        case "may" :
            out.println("31 days");
            break;
        case "june" :
            out.println("30 days");
            break;

        case "july" :
            out.println("31 days");
            break;

        case "august" :
            out.println("31 days");
            break;

        case "september" :
            out.println("30 days");
            break;

        case "october" :
            out.println("31 days");
            break;

        case "november" :
            out.println("30 days");
            break;

        case "december" :
            out.println("31 days");
            break;
        default :
            out.println("Enter a correct month");
            break;

    }
    keyboard.close();

This code works properly but it seems quite dragged-out. Is there a way i can use or operator to make this code concise. Other suggestions are also appreciated.
Thanks 😀

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To answer your question directly:

Is there a way to use logical or ( || ) in switch case?

No, it doesn’t work with the || operator. But there are other ways to do what you are after.

You can collapse all cases with the same body together:

switch (month){
    case "january" :
    case "march" :
    case "may" :
    case "july" :
    case "august" :
    case "october" :
    case "december" :
        out.println("31 days");
        break;

    case "february" :
        out.println("");
        out.print("Is it a leap year (yes/no): ");
        String leap_year = keyboard.nextLine().toLowerCase();
        if (leap_year.equals("no")){
            out.println("28 days");
            break;
        } if (leap_year.equals("yes")){
        out.println("29 days");
        break;
    } else{
        out.println("Invalid option.. Use yes/no only.");
        break;
    }

    case "april" :
    case "june" :
    case "september" :
    case "november" :
        out.println("30 days");
        break;

    default :
        out.println("Enter a correct month");
        break;
}

Or use the new switch syntax if you’re using Java 14 or newer:

switch (month) {
    case "january", "march", "may", "july", "august", "october", "december" -> out.println("31 days");
    case "february" -> {
        out.println("");
        out.print("Is it a leap year (yes/no): ");
        String leap_year = keyboard.nextLine().toLowerCase();
        if (leap_year.equals("no")) {
            out.println("28 days");
            break;
        } else if (leap_year.equals("yes")) {
            out.println("29 days");
        } else {
            out.println("Invalid option.. Use yes/no only.");
        }
        break;
    }
    case "april", "june", "september", "november" -> out.println("30 days");
    default -> out.println("Enter a correct month");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading