DEV Community

Edwin Torres
Edwin Torres

Posted on

20

Using a Switch for Days of the Week

The switch statement is a useful selection statement when there are many values that require different logic.

Here is a program that asks the user to enter a day number (1-7) and outputs the full name of that day of the week.

First, import the Scanner class (for user input), declare the class name, and declare the main method:

import java.util.Scanner;
public class Days {
  public static void main(String[] args) {
Enter fullscreen mode Exit fullscreen mode

Next, declare a Scanner variable and create the object. This object will retrieve user input later:

    Scanner in = new Scanner(System.in);
Enter fullscreen mode Exit fullscreen mode

Declare a variable to store the user input:

    int dayNum;
Enter fullscreen mode Exit fullscreen mode

Ask the user to enter a number:

    System.out.print("Enter a day number (1-7): ");
Enter fullscreen mode Exit fullscreen mode

Use the Scanner object to retrieve the user input. Note that the program will wait here until the user types a value and presses Enter:

    dayNum = in.nextInt();
Enter fullscreen mode Exit fullscreen mode

Create a switch statement that switches on the dayNum variable:

    switch(dayNum) {
Enter fullscreen mode Exit fullscreen mode

Inside the switch statement, add cases for each day. For example, the value 1 will output Monday:

      case 1:
        System.out.println("Monday");
        break;
Enter fullscreen mode Exit fullscreen mode

Here are the other cases. Note that case 5 also outputs TGIF:

      case 2:
        System.out.println("Tuesday");
        break;
      case 3:
        System.out.println("Wednesday");
        break;
      case 4:
        System.out.println("Thursday");
        break;
      case 5:
        System.out.println("Friday");
        System.out.println("TGIF!");
        break;
      case 6:
        System.out.println("Saturday");
        break;
      case 7:
        System.out.println("Sunday");
        break;  
Enter fullscreen mode Exit fullscreen mode

The last case is a default case. This case occurs when the dayNum value has a number outside the range 1-7:

      default:
        System.out.println("Invalid day number.");
        break;   
Enter fullscreen mode Exit fullscreen mode

Finally, add the closing curly brackets for the switch statement, main method, and class:

    } 
  }
}
Enter fullscreen mode Exit fullscreen mode

Here is the complete program:

import java.util.Scanner;
public class Days {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int dayNum;
    System.out.print("Enter a day number (1-7): ");
    dayNum = in.nextInt();
    switch(dayNum) {
      case 1:
        System.out.println("Monday");
        break;
      case 2:
        System.out.println("Tuesday");
        break;
      case 3:
        System.out.println("Wednesday");
        break;
      case 4:
        System.out.println("Thursday");
        break;
      case 5:
        System.out.println("Friday");
        System.out.println("TGIF!");
        break;
      case 6:
        System.out.println("Saturday");
        break;
      case 7:
        System.out.println("Sunday");
        break;       
      default:
        System.out.println("Invalid day number.");
        break;                      
    } 
  }
}
Enter fullscreen mode Exit fullscreen mode

Thanks for reading. 😃

Follow me on Twitter @realEdwinTorres for more programming tips and help.

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay